blob: ccc9d75124a6096dc0e1afb5216de31ac7829b6a [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080076
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010077static bool __read_mostly enable_vnmi = 1;
78module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
79
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020081module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070087module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
89
Xudong Hao83c3a332012-05-28 19:33:35 +080090static bool __read_mostly enable_ept_ad_bits = 1;
91module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
92
Avi Kivitya27685c2012-06-12 20:30:18 +030093static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020094module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030095
Rusty Russell476bc002012-01-13 09:32:18 +103096static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030097module_param(fasteoi, bool, S_IRUGO);
98
Yang Zhang5a717852013-04-11 19:25:16 +080099static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800100module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800101
Abel Gordonabc4fc52013-04-18 14:35:25 +0300102static bool __read_mostly enable_shadow_vmcs = 1;
103module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Rusty Russell476bc002012-01-13 09:32:18 +1030109static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Wanpeng Li20300092014-12-02 19:14:59 +0800112static u64 __read_mostly host_xss;
113
Kai Huang843e4332015-01-28 10:54:28 +0800114static bool __read_mostly enable_pml = 1;
115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100117#define MSR_TYPE_R 1
118#define MSR_TYPE_W 2
119#define MSR_TYPE_RW 3
120
121#define MSR_BITMAP_MODE_X2APIC 1
122#define MSR_BITMAP_MODE_X2APIC_APICV 2
123#define MSR_BITMAP_MODE_LM 4
124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Gleb Natapov50378782013-02-04 16:00:28 +0200134#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf4124502014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Tianyu Lan877ad952018-07-19 08:40:23 +0000191enum ept_pointers_status {
192 EPT_POINTERS_CHECK = 0,
193 EPT_POINTERS_MATCH = 1,
194 EPT_POINTERS_MISMATCH = 2
195};
196
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700197struct kvm_vmx {
198 struct kvm kvm;
199
200 unsigned int tss_addr;
201 bool ept_identity_pagetable_done;
202 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000203
204 enum ept_pointers_status ept_pointers_match;
205 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700206};
207
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200208#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300209
Liran Alon392b2f22018-06-23 02:35:01 +0300210struct vmcs_hdr {
211 u32 revision_id:31;
212 u32 shadow_vmcs:1;
213};
214
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400215struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300216 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400217 u32 abort;
218 char data[0];
219};
220
Nadav Har'Eld462b812011-05-24 15:26:10 +0300221/*
222 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
223 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
224 * loaded on this CPU (so we can clear them if the CPU goes down).
225 */
226struct loaded_vmcs {
227 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700228 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300229 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200230 bool launched;
231 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200232 unsigned long vmcs_host_cr3; /* May not match real cr3 */
233 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100234 /* Support for vnmi-less CPUs */
235 int soft_vnmi_blocked;
236 ktime_t entry_time;
237 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100238 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300239 struct list_head loaded_vmcss_on_cpu_link;
240};
241
Avi Kivity26bb0982009-09-07 11:14:12 +0300242struct shared_msr_entry {
243 unsigned index;
244 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200245 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300246};
247
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300248/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300249 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
250 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
251 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
252 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
253 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
254 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600255 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300256 * underlying hardware which will be used to run L2.
257 * This structure is packed to ensure that its layout is identical across
258 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700259 *
260 * IMPORTANT: Changing the layout of existing fields in this structure
261 * will break save/restore compatibility with older kvm releases. When
262 * adding new fields, either use space in the reserved padding* arrays
263 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300264 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300265typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300266struct __packed vmcs12 {
267 /* According to the Intel spec, a VMCS region must start with the
268 * following two fields. Then follow implementation-specific data.
269 */
Liran Alon392b2f22018-06-23 02:35:01 +0300270 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300271 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300272
Nadav Har'El27d6c862011-05-25 23:06:59 +0300273 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
274 u32 padding[7]; /* room for future expansion */
275
Nadav Har'El22bd0352011-05-25 23:05:57 +0300276 u64 io_bitmap_a;
277 u64 io_bitmap_b;
278 u64 msr_bitmap;
279 u64 vm_exit_msr_store_addr;
280 u64 vm_exit_msr_load_addr;
281 u64 vm_entry_msr_load_addr;
282 u64 tsc_offset;
283 u64 virtual_apic_page_addr;
284 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800285 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300286 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800287 u64 eoi_exit_bitmap0;
288 u64 eoi_exit_bitmap1;
289 u64 eoi_exit_bitmap2;
290 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800291 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300292 u64 guest_physical_address;
293 u64 vmcs_link_pointer;
294 u64 guest_ia32_debugctl;
295 u64 guest_ia32_pat;
296 u64 guest_ia32_efer;
297 u64 guest_ia32_perf_global_ctrl;
298 u64 guest_pdptr0;
299 u64 guest_pdptr1;
300 u64 guest_pdptr2;
301 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100302 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300303 u64 host_ia32_pat;
304 u64 host_ia32_efer;
305 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700306 u64 vmread_bitmap;
307 u64 vmwrite_bitmap;
308 u64 vm_function_control;
309 u64 eptp_list_address;
310 u64 pml_address;
311 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300312 /*
313 * To allow migration of L1 (complete with its L2 guests) between
314 * machines of different natural widths (32 or 64 bit), we cannot have
315 * unsigned long fields with no explict size. We use u64 (aliased
316 * natural_width) instead. Luckily, x86 is little-endian.
317 */
318 natural_width cr0_guest_host_mask;
319 natural_width cr4_guest_host_mask;
320 natural_width cr0_read_shadow;
321 natural_width cr4_read_shadow;
322 natural_width cr3_target_value0;
323 natural_width cr3_target_value1;
324 natural_width cr3_target_value2;
325 natural_width cr3_target_value3;
326 natural_width exit_qualification;
327 natural_width guest_linear_address;
328 natural_width guest_cr0;
329 natural_width guest_cr3;
330 natural_width guest_cr4;
331 natural_width guest_es_base;
332 natural_width guest_cs_base;
333 natural_width guest_ss_base;
334 natural_width guest_ds_base;
335 natural_width guest_fs_base;
336 natural_width guest_gs_base;
337 natural_width guest_ldtr_base;
338 natural_width guest_tr_base;
339 natural_width guest_gdtr_base;
340 natural_width guest_idtr_base;
341 natural_width guest_dr7;
342 natural_width guest_rsp;
343 natural_width guest_rip;
344 natural_width guest_rflags;
345 natural_width guest_pending_dbg_exceptions;
346 natural_width guest_sysenter_esp;
347 natural_width guest_sysenter_eip;
348 natural_width host_cr0;
349 natural_width host_cr3;
350 natural_width host_cr4;
351 natural_width host_fs_base;
352 natural_width host_gs_base;
353 natural_width host_tr_base;
354 natural_width host_gdtr_base;
355 natural_width host_idtr_base;
356 natural_width host_ia32_sysenter_esp;
357 natural_width host_ia32_sysenter_eip;
358 natural_width host_rsp;
359 natural_width host_rip;
360 natural_width paddingl[8]; /* room for future expansion */
361 u32 pin_based_vm_exec_control;
362 u32 cpu_based_vm_exec_control;
363 u32 exception_bitmap;
364 u32 page_fault_error_code_mask;
365 u32 page_fault_error_code_match;
366 u32 cr3_target_count;
367 u32 vm_exit_controls;
368 u32 vm_exit_msr_store_count;
369 u32 vm_exit_msr_load_count;
370 u32 vm_entry_controls;
371 u32 vm_entry_msr_load_count;
372 u32 vm_entry_intr_info_field;
373 u32 vm_entry_exception_error_code;
374 u32 vm_entry_instruction_len;
375 u32 tpr_threshold;
376 u32 secondary_vm_exec_control;
377 u32 vm_instruction_error;
378 u32 vm_exit_reason;
379 u32 vm_exit_intr_info;
380 u32 vm_exit_intr_error_code;
381 u32 idt_vectoring_info_field;
382 u32 idt_vectoring_error_code;
383 u32 vm_exit_instruction_len;
384 u32 vmx_instruction_info;
385 u32 guest_es_limit;
386 u32 guest_cs_limit;
387 u32 guest_ss_limit;
388 u32 guest_ds_limit;
389 u32 guest_fs_limit;
390 u32 guest_gs_limit;
391 u32 guest_ldtr_limit;
392 u32 guest_tr_limit;
393 u32 guest_gdtr_limit;
394 u32 guest_idtr_limit;
395 u32 guest_es_ar_bytes;
396 u32 guest_cs_ar_bytes;
397 u32 guest_ss_ar_bytes;
398 u32 guest_ds_ar_bytes;
399 u32 guest_fs_ar_bytes;
400 u32 guest_gs_ar_bytes;
401 u32 guest_ldtr_ar_bytes;
402 u32 guest_tr_ar_bytes;
403 u32 guest_interruptibility_info;
404 u32 guest_activity_state;
405 u32 guest_sysenter_cs;
406 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100407 u32 vmx_preemption_timer_value;
408 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300409 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800410 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300411 u16 guest_es_selector;
412 u16 guest_cs_selector;
413 u16 guest_ss_selector;
414 u16 guest_ds_selector;
415 u16 guest_fs_selector;
416 u16 guest_gs_selector;
417 u16 guest_ldtr_selector;
418 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800419 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300420 u16 host_es_selector;
421 u16 host_cs_selector;
422 u16 host_ss_selector;
423 u16 host_ds_selector;
424 u16 host_fs_selector;
425 u16 host_gs_selector;
426 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700427 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300428};
429
430/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700431 * For save/restore compatibility, the vmcs12 field offsets must not change.
432 */
433#define CHECK_OFFSET(field, loc) \
434 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
435 "Offset of " #field " in struct vmcs12 has changed.")
436
437static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300438 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700439 CHECK_OFFSET(abort, 4);
440 CHECK_OFFSET(launch_state, 8);
441 CHECK_OFFSET(io_bitmap_a, 40);
442 CHECK_OFFSET(io_bitmap_b, 48);
443 CHECK_OFFSET(msr_bitmap, 56);
444 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
445 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
446 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
447 CHECK_OFFSET(tsc_offset, 88);
448 CHECK_OFFSET(virtual_apic_page_addr, 96);
449 CHECK_OFFSET(apic_access_addr, 104);
450 CHECK_OFFSET(posted_intr_desc_addr, 112);
451 CHECK_OFFSET(ept_pointer, 120);
452 CHECK_OFFSET(eoi_exit_bitmap0, 128);
453 CHECK_OFFSET(eoi_exit_bitmap1, 136);
454 CHECK_OFFSET(eoi_exit_bitmap2, 144);
455 CHECK_OFFSET(eoi_exit_bitmap3, 152);
456 CHECK_OFFSET(xss_exit_bitmap, 160);
457 CHECK_OFFSET(guest_physical_address, 168);
458 CHECK_OFFSET(vmcs_link_pointer, 176);
459 CHECK_OFFSET(guest_ia32_debugctl, 184);
460 CHECK_OFFSET(guest_ia32_pat, 192);
461 CHECK_OFFSET(guest_ia32_efer, 200);
462 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
463 CHECK_OFFSET(guest_pdptr0, 216);
464 CHECK_OFFSET(guest_pdptr1, 224);
465 CHECK_OFFSET(guest_pdptr2, 232);
466 CHECK_OFFSET(guest_pdptr3, 240);
467 CHECK_OFFSET(guest_bndcfgs, 248);
468 CHECK_OFFSET(host_ia32_pat, 256);
469 CHECK_OFFSET(host_ia32_efer, 264);
470 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
471 CHECK_OFFSET(vmread_bitmap, 280);
472 CHECK_OFFSET(vmwrite_bitmap, 288);
473 CHECK_OFFSET(vm_function_control, 296);
474 CHECK_OFFSET(eptp_list_address, 304);
475 CHECK_OFFSET(pml_address, 312);
476 CHECK_OFFSET(cr0_guest_host_mask, 344);
477 CHECK_OFFSET(cr4_guest_host_mask, 352);
478 CHECK_OFFSET(cr0_read_shadow, 360);
479 CHECK_OFFSET(cr4_read_shadow, 368);
480 CHECK_OFFSET(cr3_target_value0, 376);
481 CHECK_OFFSET(cr3_target_value1, 384);
482 CHECK_OFFSET(cr3_target_value2, 392);
483 CHECK_OFFSET(cr3_target_value3, 400);
484 CHECK_OFFSET(exit_qualification, 408);
485 CHECK_OFFSET(guest_linear_address, 416);
486 CHECK_OFFSET(guest_cr0, 424);
487 CHECK_OFFSET(guest_cr3, 432);
488 CHECK_OFFSET(guest_cr4, 440);
489 CHECK_OFFSET(guest_es_base, 448);
490 CHECK_OFFSET(guest_cs_base, 456);
491 CHECK_OFFSET(guest_ss_base, 464);
492 CHECK_OFFSET(guest_ds_base, 472);
493 CHECK_OFFSET(guest_fs_base, 480);
494 CHECK_OFFSET(guest_gs_base, 488);
495 CHECK_OFFSET(guest_ldtr_base, 496);
496 CHECK_OFFSET(guest_tr_base, 504);
497 CHECK_OFFSET(guest_gdtr_base, 512);
498 CHECK_OFFSET(guest_idtr_base, 520);
499 CHECK_OFFSET(guest_dr7, 528);
500 CHECK_OFFSET(guest_rsp, 536);
501 CHECK_OFFSET(guest_rip, 544);
502 CHECK_OFFSET(guest_rflags, 552);
503 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
504 CHECK_OFFSET(guest_sysenter_esp, 568);
505 CHECK_OFFSET(guest_sysenter_eip, 576);
506 CHECK_OFFSET(host_cr0, 584);
507 CHECK_OFFSET(host_cr3, 592);
508 CHECK_OFFSET(host_cr4, 600);
509 CHECK_OFFSET(host_fs_base, 608);
510 CHECK_OFFSET(host_gs_base, 616);
511 CHECK_OFFSET(host_tr_base, 624);
512 CHECK_OFFSET(host_gdtr_base, 632);
513 CHECK_OFFSET(host_idtr_base, 640);
514 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
515 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
516 CHECK_OFFSET(host_rsp, 664);
517 CHECK_OFFSET(host_rip, 672);
518 CHECK_OFFSET(pin_based_vm_exec_control, 744);
519 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
520 CHECK_OFFSET(exception_bitmap, 752);
521 CHECK_OFFSET(page_fault_error_code_mask, 756);
522 CHECK_OFFSET(page_fault_error_code_match, 760);
523 CHECK_OFFSET(cr3_target_count, 764);
524 CHECK_OFFSET(vm_exit_controls, 768);
525 CHECK_OFFSET(vm_exit_msr_store_count, 772);
526 CHECK_OFFSET(vm_exit_msr_load_count, 776);
527 CHECK_OFFSET(vm_entry_controls, 780);
528 CHECK_OFFSET(vm_entry_msr_load_count, 784);
529 CHECK_OFFSET(vm_entry_intr_info_field, 788);
530 CHECK_OFFSET(vm_entry_exception_error_code, 792);
531 CHECK_OFFSET(vm_entry_instruction_len, 796);
532 CHECK_OFFSET(tpr_threshold, 800);
533 CHECK_OFFSET(secondary_vm_exec_control, 804);
534 CHECK_OFFSET(vm_instruction_error, 808);
535 CHECK_OFFSET(vm_exit_reason, 812);
536 CHECK_OFFSET(vm_exit_intr_info, 816);
537 CHECK_OFFSET(vm_exit_intr_error_code, 820);
538 CHECK_OFFSET(idt_vectoring_info_field, 824);
539 CHECK_OFFSET(idt_vectoring_error_code, 828);
540 CHECK_OFFSET(vm_exit_instruction_len, 832);
541 CHECK_OFFSET(vmx_instruction_info, 836);
542 CHECK_OFFSET(guest_es_limit, 840);
543 CHECK_OFFSET(guest_cs_limit, 844);
544 CHECK_OFFSET(guest_ss_limit, 848);
545 CHECK_OFFSET(guest_ds_limit, 852);
546 CHECK_OFFSET(guest_fs_limit, 856);
547 CHECK_OFFSET(guest_gs_limit, 860);
548 CHECK_OFFSET(guest_ldtr_limit, 864);
549 CHECK_OFFSET(guest_tr_limit, 868);
550 CHECK_OFFSET(guest_gdtr_limit, 872);
551 CHECK_OFFSET(guest_idtr_limit, 876);
552 CHECK_OFFSET(guest_es_ar_bytes, 880);
553 CHECK_OFFSET(guest_cs_ar_bytes, 884);
554 CHECK_OFFSET(guest_ss_ar_bytes, 888);
555 CHECK_OFFSET(guest_ds_ar_bytes, 892);
556 CHECK_OFFSET(guest_fs_ar_bytes, 896);
557 CHECK_OFFSET(guest_gs_ar_bytes, 900);
558 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
559 CHECK_OFFSET(guest_tr_ar_bytes, 908);
560 CHECK_OFFSET(guest_interruptibility_info, 912);
561 CHECK_OFFSET(guest_activity_state, 916);
562 CHECK_OFFSET(guest_sysenter_cs, 920);
563 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
564 CHECK_OFFSET(vmx_preemption_timer_value, 928);
565 CHECK_OFFSET(virtual_processor_id, 960);
566 CHECK_OFFSET(posted_intr_nv, 962);
567 CHECK_OFFSET(guest_es_selector, 964);
568 CHECK_OFFSET(guest_cs_selector, 966);
569 CHECK_OFFSET(guest_ss_selector, 968);
570 CHECK_OFFSET(guest_ds_selector, 970);
571 CHECK_OFFSET(guest_fs_selector, 972);
572 CHECK_OFFSET(guest_gs_selector, 974);
573 CHECK_OFFSET(guest_ldtr_selector, 976);
574 CHECK_OFFSET(guest_tr_selector, 978);
575 CHECK_OFFSET(guest_intr_status, 980);
576 CHECK_OFFSET(host_es_selector, 982);
577 CHECK_OFFSET(host_cs_selector, 984);
578 CHECK_OFFSET(host_ss_selector, 986);
579 CHECK_OFFSET(host_ds_selector, 988);
580 CHECK_OFFSET(host_fs_selector, 990);
581 CHECK_OFFSET(host_gs_selector, 992);
582 CHECK_OFFSET(host_tr_selector, 994);
583 CHECK_OFFSET(guest_pml_index, 996);
584}
585
586/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300587 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
588 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
589 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700590 *
591 * IMPORTANT: Changing this value will break save/restore compatibility with
592 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300593 */
594#define VMCS12_REVISION 0x11e57ed0
595
596/*
597 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
598 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
599 * current implementation, 4K are reserved to avoid future complications.
600 */
601#define VMCS12_SIZE 0x1000
602
603/*
Jim Mattson5b157062017-12-22 12:11:12 -0800604 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
605 * supported VMCS12 field encoding.
606 */
607#define VMCS12_MAX_FIELD_INDEX 0x17
608
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100609struct nested_vmx_msrs {
610 /*
611 * We only store the "true" versions of the VMX capability MSRs. We
612 * generate the "non-true" versions by setting the must-be-1 bits
613 * according to the SDM.
614 */
615 u32 procbased_ctls_low;
616 u32 procbased_ctls_high;
617 u32 secondary_ctls_low;
618 u32 secondary_ctls_high;
619 u32 pinbased_ctls_low;
620 u32 pinbased_ctls_high;
621 u32 exit_ctls_low;
622 u32 exit_ctls_high;
623 u32 entry_ctls_low;
624 u32 entry_ctls_high;
625 u32 misc_low;
626 u32 misc_high;
627 u32 ept_caps;
628 u32 vpid_caps;
629 u64 basic;
630 u64 cr0_fixed0;
631 u64 cr0_fixed1;
632 u64 cr4_fixed0;
633 u64 cr4_fixed1;
634 u64 vmcs_enum;
635 u64 vmfunc_controls;
636};
637
Jim Mattson5b157062017-12-22 12:11:12 -0800638/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300639 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
640 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
641 */
642struct nested_vmx {
643 /* Has the level1 guest done vmxon? */
644 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400645 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400646 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300647
648 /* The guest-physical address of the current VMCS L1 keeps for L2 */
649 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700650 /*
651 * Cache of the guest's VMCS, existing outside of guest memory.
652 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700653 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700654 */
655 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300656 /*
Liran Alon61ada742018-06-23 02:35:08 +0300657 * Cache of the guest's shadow VMCS, existing outside of guest
658 * memory. Loaded from guest memory during VM entry. Flushed
659 * to guest memory during VM exit.
660 */
661 struct vmcs12 *cached_shadow_vmcs12;
662 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300663 * Indicates if the shadow vmcs must be updated with the
664 * data hold by vmcs12
665 */
666 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100667 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300668
Jim Mattson8d860bb2018-05-09 16:56:05 -0400669 bool change_vmcs01_virtual_apic_mode;
670
Nadav Har'El644d7112011-05-25 23:12:35 +0300671 /* L2 must run next, and mustn't decide to exit to L1. */
672 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600673
674 struct loaded_vmcs vmcs02;
675
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300676 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600677 * Guest pages referred to in the vmcs02 with host-physical
678 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300679 */
680 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800681 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800682 struct page *pi_desc_page;
683 struct pi_desc *pi_desc;
684 bool pi_pending;
685 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100686
687 struct hrtimer preemption_timer;
688 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200689
690 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
691 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800692
Wanpeng Li5c614b32015-10-13 09:18:36 -0700693 u16 vpid02;
694 u16 last_vpid;
695
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100696 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200697
698 /* SMM related state */
699 struct {
700 /* in VMX operation on SMM entry? */
701 bool vmxon;
702 /* in guest mode on SMM entry? */
703 bool guest_mode;
704 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300705};
706
Yang Zhang01e439b2013-04-11 19:25:12 +0800707#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800708#define POSTED_INTR_SN 1
709
Yang Zhang01e439b2013-04-11 19:25:12 +0800710/* Posted-Interrupt Descriptor */
711struct pi_desc {
712 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800713 union {
714 struct {
715 /* bit 256 - Outstanding Notification */
716 u16 on : 1,
717 /* bit 257 - Suppress Notification */
718 sn : 1,
719 /* bit 271:258 - Reserved */
720 rsvd_1 : 14;
721 /* bit 279:272 - Notification Vector */
722 u8 nv;
723 /* bit 287:280 - Reserved */
724 u8 rsvd_2;
725 /* bit 319:288 - Notification Destination */
726 u32 ndst;
727 };
728 u64 control;
729 };
730 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800731} __aligned(64);
732
Yang Zhanga20ed542013-04-11 19:25:15 +0800733static bool pi_test_and_set_on(struct pi_desc *pi_desc)
734{
735 return test_and_set_bit(POSTED_INTR_ON,
736 (unsigned long *)&pi_desc->control);
737}
738
739static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
740{
741 return test_and_clear_bit(POSTED_INTR_ON,
742 (unsigned long *)&pi_desc->control);
743}
744
745static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
746{
747 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
748}
749
Feng Wuebbfc762015-09-18 22:29:46 +0800750static inline void pi_clear_sn(struct pi_desc *pi_desc)
751{
752 return clear_bit(POSTED_INTR_SN,
753 (unsigned long *)&pi_desc->control);
754}
755
756static inline void pi_set_sn(struct pi_desc *pi_desc)
757{
758 return set_bit(POSTED_INTR_SN,
759 (unsigned long *)&pi_desc->control);
760}
761
Paolo Bonziniad361092016-09-20 16:15:05 +0200762static inline void pi_clear_on(struct pi_desc *pi_desc)
763{
764 clear_bit(POSTED_INTR_ON,
765 (unsigned long *)&pi_desc->control);
766}
767
Feng Wuebbfc762015-09-18 22:29:46 +0800768static inline int pi_test_on(struct pi_desc *pi_desc)
769{
770 return test_bit(POSTED_INTR_ON,
771 (unsigned long *)&pi_desc->control);
772}
773
774static inline int pi_test_sn(struct pi_desc *pi_desc)
775{
776 return test_bit(POSTED_INTR_SN,
777 (unsigned long *)&pi_desc->control);
778}
779
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400780struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000781 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300782 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300783 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100784 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300785 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200786 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200787 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300788 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400789 int nmsrs;
790 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800791 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400792#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300793 u64 msr_host_kernel_gs_base;
794 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400795#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100796
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100797 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100798 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100799
Gleb Natapov2961e8762013-11-25 15:37:13 +0200800 u32 vm_entry_controls_shadow;
801 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200802 u32 secondary_exec_control;
803
Nadav Har'Eld462b812011-05-24 15:26:10 +0300804 /*
805 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
806 * non-nested (L1) guest, it always points to vmcs01. For a nested
807 * guest (L2), it points to a different VMCS.
808 */
809 struct loaded_vmcs vmcs01;
810 struct loaded_vmcs *loaded_vmcs;
811 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300812 struct msr_autoload {
813 unsigned nr;
814 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
815 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
816 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400817 struct {
818 int loaded;
819 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300820#ifdef CONFIG_X86_64
821 u16 ds_sel, es_sel;
822#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200823 int gs_ldt_reload_needed;
824 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400825 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200826 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300827 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300828 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300829 struct kvm_segment segs[8];
830 } rmode;
831 struct {
832 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300833 struct kvm_save_segment {
834 u16 selector;
835 unsigned long base;
836 u32 limit;
837 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300838 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300839 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800840 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300841 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200842
Andi Kleena0861c02009-06-08 17:37:09 +0800843 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800844
Yang Zhang01e439b2013-04-11 19:25:12 +0800845 /* Posted interrupt descriptor */
846 struct pi_desc pi_desc;
847
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300848 /* Support for a guest hypervisor (nested VMX) */
849 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200850
851 /* Dynamic PLE window. */
852 int ple_window;
853 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800854
855 /* Support for PML */
856#define PML_ENTITY_NUM 512
857 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800858
Yunhong Jiang64672c92016-06-13 14:19:59 -0700859 /* apic deadline value in host tsc */
860 u64 hv_deadline_tsc;
861
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800862 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800863
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800864 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800865
Wanpeng Li74c55932017-11-29 01:31:20 -0800866 unsigned long host_debugctlmsr;
867
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800868 /*
869 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
870 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
871 * in msr_ia32_feature_control_valid_bits.
872 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800873 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800874 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +0000875 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400876};
877
Avi Kivity2fb92db2011-04-27 19:42:18 +0300878enum segment_cache_field {
879 SEG_FIELD_SEL = 0,
880 SEG_FIELD_BASE = 1,
881 SEG_FIELD_LIMIT = 2,
882 SEG_FIELD_AR = 3,
883
884 SEG_FIELD_NR = 4
885};
886
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700887static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
888{
889 return container_of(kvm, struct kvm_vmx, kvm);
890}
891
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400892static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
893{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000894 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400895}
896
Feng Wuefc64402015-09-18 22:29:51 +0800897static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
898{
899 return &(to_vmx(vcpu)->pi_desc);
900}
901
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800902#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300903#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800904#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
905#define FIELD64(number, name) \
906 FIELD(number, name), \
907 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300908
Abel Gordon4607c2d2013-04-18 14:35:55 +0300909
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100910static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100911#define SHADOW_FIELD_RO(x) x,
912#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300913};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400914static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300915 ARRAY_SIZE(shadow_read_only_fields);
916
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100917static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100918#define SHADOW_FIELD_RW(x) x,
919#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300920};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400921static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300922 ARRAY_SIZE(shadow_read_write_fields);
923
Mathias Krause772e0312012-08-30 01:30:19 +0200924static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300925 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800926 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300927 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
928 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
929 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
930 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
931 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
932 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
933 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
934 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800935 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400936 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300937 FIELD(HOST_ES_SELECTOR, host_es_selector),
938 FIELD(HOST_CS_SELECTOR, host_cs_selector),
939 FIELD(HOST_SS_SELECTOR, host_ss_selector),
940 FIELD(HOST_DS_SELECTOR, host_ds_selector),
941 FIELD(HOST_FS_SELECTOR, host_fs_selector),
942 FIELD(HOST_GS_SELECTOR, host_gs_selector),
943 FIELD(HOST_TR_SELECTOR, host_tr_selector),
944 FIELD64(IO_BITMAP_A, io_bitmap_a),
945 FIELD64(IO_BITMAP_B, io_bitmap_b),
946 FIELD64(MSR_BITMAP, msr_bitmap),
947 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
948 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
949 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700950 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300951 FIELD64(TSC_OFFSET, tsc_offset),
952 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
953 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800954 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400955 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300956 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800957 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
958 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
959 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
960 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400961 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700962 FIELD64(VMREAD_BITMAP, vmread_bitmap),
963 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800964 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300965 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
966 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
967 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
968 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
969 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
970 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
971 FIELD64(GUEST_PDPTR0, guest_pdptr0),
972 FIELD64(GUEST_PDPTR1, guest_pdptr1),
973 FIELD64(GUEST_PDPTR2, guest_pdptr2),
974 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100975 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300976 FIELD64(HOST_IA32_PAT, host_ia32_pat),
977 FIELD64(HOST_IA32_EFER, host_ia32_efer),
978 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
979 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
980 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
981 FIELD(EXCEPTION_BITMAP, exception_bitmap),
982 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
983 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
984 FIELD(CR3_TARGET_COUNT, cr3_target_count),
985 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
986 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
987 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
988 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
989 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
990 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
991 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
992 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
993 FIELD(TPR_THRESHOLD, tpr_threshold),
994 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
995 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
996 FIELD(VM_EXIT_REASON, vm_exit_reason),
997 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
998 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
999 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1000 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1001 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1002 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1003 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1004 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1005 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1006 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1007 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1008 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1009 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1010 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1011 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1012 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1013 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1014 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1015 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1016 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1017 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1018 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1019 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1020 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1021 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1022 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1023 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1024 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001025 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001026 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1027 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1028 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1029 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1030 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1031 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1032 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1033 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1034 FIELD(EXIT_QUALIFICATION, exit_qualification),
1035 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1036 FIELD(GUEST_CR0, guest_cr0),
1037 FIELD(GUEST_CR3, guest_cr3),
1038 FIELD(GUEST_CR4, guest_cr4),
1039 FIELD(GUEST_ES_BASE, guest_es_base),
1040 FIELD(GUEST_CS_BASE, guest_cs_base),
1041 FIELD(GUEST_SS_BASE, guest_ss_base),
1042 FIELD(GUEST_DS_BASE, guest_ds_base),
1043 FIELD(GUEST_FS_BASE, guest_fs_base),
1044 FIELD(GUEST_GS_BASE, guest_gs_base),
1045 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1046 FIELD(GUEST_TR_BASE, guest_tr_base),
1047 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1048 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1049 FIELD(GUEST_DR7, guest_dr7),
1050 FIELD(GUEST_RSP, guest_rsp),
1051 FIELD(GUEST_RIP, guest_rip),
1052 FIELD(GUEST_RFLAGS, guest_rflags),
1053 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1054 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1055 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1056 FIELD(HOST_CR0, host_cr0),
1057 FIELD(HOST_CR3, host_cr3),
1058 FIELD(HOST_CR4, host_cr4),
1059 FIELD(HOST_FS_BASE, host_fs_base),
1060 FIELD(HOST_GS_BASE, host_gs_base),
1061 FIELD(HOST_TR_BASE, host_tr_base),
1062 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1063 FIELD(HOST_IDTR_BASE, host_idtr_base),
1064 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1065 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1066 FIELD(HOST_RSP, host_rsp),
1067 FIELD(HOST_RIP, host_rip),
1068};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001069
1070static inline short vmcs_field_to_offset(unsigned long field)
1071{
Dan Williams085331d2018-01-31 17:47:03 -08001072 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1073 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001074 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001075
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001076 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001077 return -ENOENT;
1078
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001079 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001080 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001081 return -ENOENT;
1082
Linus Torvalds15303ba2018-02-10 13:16:35 -08001083 index = array_index_nospec(index, size);
1084 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001085 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001086 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001087 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001088}
1089
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001090static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1091{
David Matlack4f2777b2016-07-13 17:16:37 -07001092 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001093}
1094
Liran Alon61ada742018-06-23 02:35:08 +03001095static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1096{
1097 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1098}
1099
Peter Feiner995f00a2017-06-30 17:26:32 -07001100static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001101static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001102static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001103static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001104static void vmx_set_segment(struct kvm_vcpu *vcpu,
1105 struct kvm_segment *var, int seg);
1106static void vmx_get_segment(struct kvm_vcpu *vcpu,
1107 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001108static bool guest_state_valid(struct kvm_vcpu *vcpu);
1109static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001110static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001111static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1112static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1113static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1114 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001115static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001116static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1117 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001118
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1120static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001121/*
1122 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1123 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1124 */
1125static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126
Feng Wubf9f6ac2015-09-18 22:29:55 +08001127/*
1128 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1129 * can find which vCPU should be waken up.
1130 */
1131static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1132static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1133
Radim Krčmář23611332016-09-29 22:41:33 +02001134enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001135 VMX_VMREAD_BITMAP,
1136 VMX_VMWRITE_BITMAP,
1137 VMX_BITMAP_NR
1138};
1139
1140static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1141
Radim Krčmář23611332016-09-29 22:41:33 +02001142#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1143#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001144
Avi Kivity110312c2010-12-21 12:54:20 +02001145static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001146static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001147
Sheng Yang2384d2b2008-01-17 15:14:33 +08001148static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1149static DEFINE_SPINLOCK(vmx_vpid_lock);
1150
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001151static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 int size;
1153 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001154 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001156 u32 pin_based_exec_ctrl;
1157 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001158 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001159 u32 vmexit_ctrl;
1160 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001161 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001162} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163
Hannes Ederefff9e52008-11-28 17:02:06 +01001164static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001165 u32 ept;
1166 u32 vpid;
1167} vmx_capability;
1168
Avi Kivity6aa8b732006-12-10 02:21:36 -08001169#define VMX_SEGMENT_FIELD(seg) \
1170 [VCPU_SREG_##seg] = { \
1171 .selector = GUEST_##seg##_SELECTOR, \
1172 .base = GUEST_##seg##_BASE, \
1173 .limit = GUEST_##seg##_LIMIT, \
1174 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1175 }
1176
Mathias Krause772e0312012-08-30 01:30:19 +02001177static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 unsigned selector;
1179 unsigned base;
1180 unsigned limit;
1181 unsigned ar_bytes;
1182} kvm_vmx_segment_fields[] = {
1183 VMX_SEGMENT_FIELD(CS),
1184 VMX_SEGMENT_FIELD(DS),
1185 VMX_SEGMENT_FIELD(ES),
1186 VMX_SEGMENT_FIELD(FS),
1187 VMX_SEGMENT_FIELD(GS),
1188 VMX_SEGMENT_FIELD(SS),
1189 VMX_SEGMENT_FIELD(TR),
1190 VMX_SEGMENT_FIELD(LDTR),
1191};
1192
Avi Kivity26bb0982009-09-07 11:14:12 +03001193static u64 host_efer;
1194
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001195static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1196
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001197/*
Brian Gerst8c065852010-07-17 09:03:26 -04001198 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001199 * away by decrementing the array size.
1200 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001202#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001203 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001205 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001208DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1209
1210#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1211
1212#define KVM_EVMCS_VERSION 1
1213
1214#if IS_ENABLED(CONFIG_HYPERV)
1215static bool __read_mostly enlightened_vmcs = true;
1216module_param(enlightened_vmcs, bool, 0444);
1217
1218static inline void evmcs_write64(unsigned long field, u64 value)
1219{
1220 u16 clean_field;
1221 int offset = get_evmcs_offset(field, &clean_field);
1222
1223 if (offset < 0)
1224 return;
1225
1226 *(u64 *)((char *)current_evmcs + offset) = value;
1227
1228 current_evmcs->hv_clean_fields &= ~clean_field;
1229}
1230
1231static inline void evmcs_write32(unsigned long field, u32 value)
1232{
1233 u16 clean_field;
1234 int offset = get_evmcs_offset(field, &clean_field);
1235
1236 if (offset < 0)
1237 return;
1238
1239 *(u32 *)((char *)current_evmcs + offset) = value;
1240 current_evmcs->hv_clean_fields &= ~clean_field;
1241}
1242
1243static inline void evmcs_write16(unsigned long field, u16 value)
1244{
1245 u16 clean_field;
1246 int offset = get_evmcs_offset(field, &clean_field);
1247
1248 if (offset < 0)
1249 return;
1250
1251 *(u16 *)((char *)current_evmcs + offset) = value;
1252 current_evmcs->hv_clean_fields &= ~clean_field;
1253}
1254
1255static inline u64 evmcs_read64(unsigned long field)
1256{
1257 int offset = get_evmcs_offset(field, NULL);
1258
1259 if (offset < 0)
1260 return 0;
1261
1262 return *(u64 *)((char *)current_evmcs + offset);
1263}
1264
1265static inline u32 evmcs_read32(unsigned long field)
1266{
1267 int offset = get_evmcs_offset(field, NULL);
1268
1269 if (offset < 0)
1270 return 0;
1271
1272 return *(u32 *)((char *)current_evmcs + offset);
1273}
1274
1275static inline u16 evmcs_read16(unsigned long field)
1276{
1277 int offset = get_evmcs_offset(field, NULL);
1278
1279 if (offset < 0)
1280 return 0;
1281
1282 return *(u16 *)((char *)current_evmcs + offset);
1283}
1284
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001285static inline void evmcs_touch_msr_bitmap(void)
1286{
1287 if (unlikely(!current_evmcs))
1288 return;
1289
1290 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1291 current_evmcs->hv_clean_fields &=
1292 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1293}
1294
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001295static void evmcs_load(u64 phys_addr)
1296{
1297 struct hv_vp_assist_page *vp_ap =
1298 hv_get_vp_assist_page(smp_processor_id());
1299
1300 vp_ap->current_nested_vmcs = phys_addr;
1301 vp_ap->enlighten_vmentry = 1;
1302}
1303
1304static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1305{
1306 /*
1307 * Enlightened VMCSv1 doesn't support these:
1308 *
1309 * POSTED_INTR_NV = 0x00000002,
1310 * GUEST_INTR_STATUS = 0x00000810,
1311 * APIC_ACCESS_ADDR = 0x00002014,
1312 * POSTED_INTR_DESC_ADDR = 0x00002016,
1313 * EOI_EXIT_BITMAP0 = 0x0000201c,
1314 * EOI_EXIT_BITMAP1 = 0x0000201e,
1315 * EOI_EXIT_BITMAP2 = 0x00002020,
1316 * EOI_EXIT_BITMAP3 = 0x00002022,
1317 */
1318 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1319 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1320 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1321 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1322 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1323 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1324 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1325
1326 /*
1327 * GUEST_PML_INDEX = 0x00000812,
1328 * PML_ADDRESS = 0x0000200e,
1329 */
1330 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1331
1332 /* VM_FUNCTION_CONTROL = 0x00002018, */
1333 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1334
1335 /*
1336 * EPTP_LIST_ADDRESS = 0x00002024,
1337 * VMREAD_BITMAP = 0x00002026,
1338 * VMWRITE_BITMAP = 0x00002028,
1339 */
1340 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1341
1342 /*
1343 * TSC_MULTIPLIER = 0x00002032,
1344 */
1345 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1346
1347 /*
1348 * PLE_GAP = 0x00004020,
1349 * PLE_WINDOW = 0x00004022,
1350 */
1351 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1352
1353 /*
1354 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1355 */
1356 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1357
1358 /*
1359 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1360 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1361 */
1362 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1363 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1364
1365 /*
1366 * Currently unsupported in KVM:
1367 * GUEST_IA32_RTIT_CTL = 0x00002814,
1368 */
1369}
Tianyu Lan877ad952018-07-19 08:40:23 +00001370
1371/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1372static void check_ept_pointer_match(struct kvm *kvm)
1373{
1374 struct kvm_vcpu *vcpu;
1375 u64 tmp_eptp = INVALID_PAGE;
1376 int i;
1377
1378 kvm_for_each_vcpu(i, vcpu, kvm) {
1379 if (!VALID_PAGE(tmp_eptp)) {
1380 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1381 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1382 to_kvm_vmx(kvm)->ept_pointers_match
1383 = EPT_POINTERS_MISMATCH;
1384 return;
1385 }
1386 }
1387
1388 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1389}
1390
1391static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1392{
1393 int ret;
1394
1395 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1396
1397 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1398 check_ept_pointer_match(kvm);
1399
1400 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1401 ret = -ENOTSUPP;
1402 goto out;
1403 }
1404
1405 ret = hyperv_flush_guest_mapping(
1406 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1407
1408out:
1409 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1410 return ret;
1411}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001412#else /* !IS_ENABLED(CONFIG_HYPERV) */
1413static inline void evmcs_write64(unsigned long field, u64 value) {}
1414static inline void evmcs_write32(unsigned long field, u32 value) {}
1415static inline void evmcs_write16(unsigned long field, u16 value) {}
1416static inline u64 evmcs_read64(unsigned long field) { return 0; }
1417static inline u32 evmcs_read32(unsigned long field) { return 0; }
1418static inline u16 evmcs_read16(unsigned long field) { return 0; }
1419static inline void evmcs_load(u64 phys_addr) {}
1420static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001421static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001422#endif /* IS_ENABLED(CONFIG_HYPERV) */
1423
Jan Kiszka5bb16012016-02-09 20:14:21 +01001424static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425{
1426 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1427 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001428 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1429}
1430
Jan Kiszka6f054852016-02-09 20:15:18 +01001431static inline bool is_debug(u32 intr_info)
1432{
1433 return is_exception_n(intr_info, DB_VECTOR);
1434}
1435
1436static inline bool is_breakpoint(u32 intr_info)
1437{
1438 return is_exception_n(intr_info, BP_VECTOR);
1439}
1440
Jan Kiszka5bb16012016-02-09 20:14:21 +01001441static inline bool is_page_fault(u32 intr_info)
1442{
1443 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444}
1445
Gui Jianfeng31299942010-03-15 17:29:09 +08001446static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001447{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001448 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001449}
1450
Gui Jianfeng31299942010-03-15 17:29:09 +08001451static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001452{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001453 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001454}
1455
Liran Alon9e869482018-03-12 13:12:51 +02001456static inline bool is_gp_fault(u32 intr_info)
1457{
1458 return is_exception_n(intr_info, GP_VECTOR);
1459}
1460
Gui Jianfeng31299942010-03-15 17:29:09 +08001461static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462{
1463 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1464 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1465}
1466
Gui Jianfeng31299942010-03-15 17:29:09 +08001467static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001468{
1469 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1470 INTR_INFO_VALID_MASK)) ==
1471 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1472}
1473
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001474/* Undocumented: icebp/int1 */
1475static inline bool is_icebp(u32 intr_info)
1476{
1477 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1478 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1479}
1480
Gui Jianfeng31299942010-03-15 17:29:09 +08001481static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001482{
Sheng Yang04547152009-04-01 15:52:31 +08001483 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001484}
1485
Gui Jianfeng31299942010-03-15 17:29:09 +08001486static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001487{
Sheng Yang04547152009-04-01 15:52:31 +08001488 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001489}
1490
Paolo Bonzini35754c92015-07-29 12:05:37 +02001491static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001492{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001493 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001494}
1495
Gui Jianfeng31299942010-03-15 17:29:09 +08001496static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001497{
Sheng Yang04547152009-04-01 15:52:31 +08001498 return vmcs_config.cpu_based_exec_ctrl &
1499 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001500}
1501
Avi Kivity774ead32007-12-26 13:57:04 +02001502static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001503{
Sheng Yang04547152009-04-01 15:52:31 +08001504 return vmcs_config.cpu_based_2nd_exec_ctrl &
1505 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1506}
1507
Yang Zhang8d146952013-01-25 10:18:50 +08001508static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1509{
1510 return vmcs_config.cpu_based_2nd_exec_ctrl &
1511 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1512}
1513
Yang Zhang83d4c282013-01-25 10:18:49 +08001514static inline bool cpu_has_vmx_apic_register_virt(void)
1515{
1516 return vmcs_config.cpu_based_2nd_exec_ctrl &
1517 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1518}
1519
Yang Zhangc7c9c562013-01-25 10:18:51 +08001520static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1521{
1522 return vmcs_config.cpu_based_2nd_exec_ctrl &
1523 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1524}
1525
Yunhong Jiang64672c92016-06-13 14:19:59 -07001526/*
1527 * Comment's format: document - errata name - stepping - processor name.
1528 * Refer from
1529 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1530 */
1531static u32 vmx_preemption_cpu_tfms[] = {
1532/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
15330x000206E6,
1534/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1535/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1536/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
15370x00020652,
1538/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
15390x00020655,
1540/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1541/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1542/*
1543 * 320767.pdf - AAP86 - B1 -
1544 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1545 */
15460x000106E5,
1547/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
15480x000106A0,
1549/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
15500x000106A1,
1551/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15520x000106A4,
1553 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1554 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1555 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15560x000106A5,
1557};
1558
1559static inline bool cpu_has_broken_vmx_preemption_timer(void)
1560{
1561 u32 eax = cpuid_eax(0x00000001), i;
1562
1563 /* Clear the reserved bits */
1564 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001565 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001566 if (eax == vmx_preemption_cpu_tfms[i])
1567 return true;
1568
1569 return false;
1570}
1571
1572static inline bool cpu_has_vmx_preemption_timer(void)
1573{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001574 return vmcs_config.pin_based_exec_ctrl &
1575 PIN_BASED_VMX_PREEMPTION_TIMER;
1576}
1577
Yang Zhang01e439b2013-04-11 19:25:12 +08001578static inline bool cpu_has_vmx_posted_intr(void)
1579{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001580 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1581 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001582}
1583
1584static inline bool cpu_has_vmx_apicv(void)
1585{
1586 return cpu_has_vmx_apic_register_virt() &&
1587 cpu_has_vmx_virtual_intr_delivery() &&
1588 cpu_has_vmx_posted_intr();
1589}
1590
Sheng Yang04547152009-04-01 15:52:31 +08001591static inline bool cpu_has_vmx_flexpriority(void)
1592{
1593 return cpu_has_vmx_tpr_shadow() &&
1594 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001595}
1596
Marcelo Tosattie7997942009-06-11 12:07:40 -03001597static inline bool cpu_has_vmx_ept_execute_only(void)
1598{
Gui Jianfeng31299942010-03-15 17:29:09 +08001599 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001600}
1601
Marcelo Tosattie7997942009-06-11 12:07:40 -03001602static inline bool cpu_has_vmx_ept_2m_page(void)
1603{
Gui Jianfeng31299942010-03-15 17:29:09 +08001604 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001605}
1606
Sheng Yang878403b2010-01-05 19:02:29 +08001607static inline bool cpu_has_vmx_ept_1g_page(void)
1608{
Gui Jianfeng31299942010-03-15 17:29:09 +08001609 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001610}
1611
Sheng Yang4bc9b982010-06-02 14:05:24 +08001612static inline bool cpu_has_vmx_ept_4levels(void)
1613{
1614 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1615}
1616
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001617static inline bool cpu_has_vmx_ept_mt_wb(void)
1618{
1619 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1620}
1621
Yu Zhang855feb62017-08-24 20:27:55 +08001622static inline bool cpu_has_vmx_ept_5levels(void)
1623{
1624 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1625}
1626
Xudong Hao83c3a332012-05-28 19:33:35 +08001627static inline bool cpu_has_vmx_ept_ad_bits(void)
1628{
1629 return vmx_capability.ept & VMX_EPT_AD_BIT;
1630}
1631
Gui Jianfeng31299942010-03-15 17:29:09 +08001632static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001633{
Gui Jianfeng31299942010-03-15 17:29:09 +08001634 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001635}
1636
Gui Jianfeng31299942010-03-15 17:29:09 +08001637static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001638{
Gui Jianfeng31299942010-03-15 17:29:09 +08001639 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001640}
1641
Liran Aloncd9a4912018-05-22 17:16:15 +03001642static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1643{
1644 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1645}
1646
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001647static inline bool cpu_has_vmx_invvpid_single(void)
1648{
1649 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1650}
1651
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001652static inline bool cpu_has_vmx_invvpid_global(void)
1653{
1654 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1655}
1656
Wanpeng Li08d839c2017-03-23 05:30:08 -07001657static inline bool cpu_has_vmx_invvpid(void)
1658{
1659 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1660}
1661
Gui Jianfeng31299942010-03-15 17:29:09 +08001662static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001663{
Sheng Yang04547152009-04-01 15:52:31 +08001664 return vmcs_config.cpu_based_2nd_exec_ctrl &
1665 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001666}
1667
Gui Jianfeng31299942010-03-15 17:29:09 +08001668static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001669{
1670 return vmcs_config.cpu_based_2nd_exec_ctrl &
1671 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1672}
1673
Gui Jianfeng31299942010-03-15 17:29:09 +08001674static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001675{
1676 return vmcs_config.cpu_based_2nd_exec_ctrl &
1677 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1678}
1679
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001680static inline bool cpu_has_vmx_basic_inout(void)
1681{
1682 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1683}
1684
Paolo Bonzini35754c92015-07-29 12:05:37 +02001685static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001686{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001687 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001688}
1689
Gui Jianfeng31299942010-03-15 17:29:09 +08001690static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001691{
Sheng Yang04547152009-04-01 15:52:31 +08001692 return vmcs_config.cpu_based_2nd_exec_ctrl &
1693 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001694}
1695
Gui Jianfeng31299942010-03-15 17:29:09 +08001696static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001697{
1698 return vmcs_config.cpu_based_2nd_exec_ctrl &
1699 SECONDARY_EXEC_RDTSCP;
1700}
1701
Mao, Junjiead756a12012-07-02 01:18:48 +00001702static inline bool cpu_has_vmx_invpcid(void)
1703{
1704 return vmcs_config.cpu_based_2nd_exec_ctrl &
1705 SECONDARY_EXEC_ENABLE_INVPCID;
1706}
1707
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001708static inline bool cpu_has_virtual_nmis(void)
1709{
1710 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1711}
1712
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001713static inline bool cpu_has_vmx_wbinvd_exit(void)
1714{
1715 return vmcs_config.cpu_based_2nd_exec_ctrl &
1716 SECONDARY_EXEC_WBINVD_EXITING;
1717}
1718
Abel Gordonabc4fc52013-04-18 14:35:25 +03001719static inline bool cpu_has_vmx_shadow_vmcs(void)
1720{
1721 u64 vmx_msr;
1722 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1723 /* check if the cpu supports writing r/o exit information fields */
1724 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1725 return false;
1726
1727 return vmcs_config.cpu_based_2nd_exec_ctrl &
1728 SECONDARY_EXEC_SHADOW_VMCS;
1729}
1730
Kai Huang843e4332015-01-28 10:54:28 +08001731static inline bool cpu_has_vmx_pml(void)
1732{
1733 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1734}
1735
Haozhong Zhang64903d62015-10-20 15:39:09 +08001736static inline bool cpu_has_vmx_tsc_scaling(void)
1737{
1738 return vmcs_config.cpu_based_2nd_exec_ctrl &
1739 SECONDARY_EXEC_TSC_SCALING;
1740}
1741
Bandan Das2a499e42017-08-03 15:54:41 -04001742static inline bool cpu_has_vmx_vmfunc(void)
1743{
1744 return vmcs_config.cpu_based_2nd_exec_ctrl &
1745 SECONDARY_EXEC_ENABLE_VMFUNC;
1746}
1747
Sean Christopherson64f7a112018-04-30 10:01:06 -07001748static bool vmx_umip_emulated(void)
1749{
1750 return vmcs_config.cpu_based_2nd_exec_ctrl &
1751 SECONDARY_EXEC_DESC;
1752}
1753
Sheng Yang04547152009-04-01 15:52:31 +08001754static inline bool report_flexpriority(void)
1755{
1756 return flexpriority_enabled;
1757}
1758
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001759static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1760{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001761 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001762}
1763
Jim Mattsonf4160e42018-05-29 09:11:33 -07001764/*
1765 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1766 * to modify any valid field of the VMCS, or are the VM-exit
1767 * information fields read-only?
1768 */
1769static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1770{
1771 return to_vmx(vcpu)->nested.msrs.misc_low &
1772 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1773}
1774
Marc Orr04473782018-06-20 17:21:29 -07001775static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1776{
1777 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1778}
1779
1780static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1781{
1782 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1783 CPU_BASED_MONITOR_TRAP_FLAG;
1784}
1785
Liran Alonfa97d7d2018-07-18 14:07:59 +02001786static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1787{
1788 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1789 SECONDARY_EXEC_SHADOW_VMCS;
1790}
1791
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001792static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1793{
1794 return vmcs12->cpu_based_vm_exec_control & bit;
1795}
1796
1797static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1798{
1799 return (vmcs12->cpu_based_vm_exec_control &
1800 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1801 (vmcs12->secondary_vm_exec_control & bit);
1802}
1803
Jan Kiszkaf4124502014-03-07 20:03:13 +01001804static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1805{
1806 return vmcs12->pin_based_vm_exec_control &
1807 PIN_BASED_VMX_PREEMPTION_TIMER;
1808}
1809
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001810static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1811{
1812 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1813}
1814
1815static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1816{
1817 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1818}
1819
Nadav Har'El155a97a2013-08-05 11:07:16 +03001820static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1821{
1822 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1823}
1824
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001825static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1826{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001827 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001828}
1829
Bandan Dasc5f983f2017-05-05 15:25:14 -04001830static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1831{
1832 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1833}
1834
Wincy Vanf2b93282015-02-03 23:56:03 +08001835static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1836{
1837 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1838}
1839
Wanpeng Li5c614b32015-10-13 09:18:36 -07001840static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1841{
1842 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1843}
1844
Wincy Van82f0dd42015-02-03 23:57:18 +08001845static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1846{
1847 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1848}
1849
Wincy Van608406e2015-02-03 23:57:51 +08001850static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1851{
1852 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1853}
1854
Wincy Van705699a2015-02-03 23:58:17 +08001855static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1856{
1857 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1858}
1859
Bandan Das27c42a12017-08-03 15:54:42 -04001860static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1861{
1862 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1863}
1864
Bandan Das41ab9372017-08-03 15:54:43 -04001865static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1866{
1867 return nested_cpu_has_vmfunc(vmcs12) &&
1868 (vmcs12->vm_function_control &
1869 VMX_VMFUNC_EPTP_SWITCHING);
1870}
1871
Liran Alonf792d272018-06-23 02:35:05 +03001872static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
1873{
1874 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
1875}
1876
Jim Mattsonef85b672016-12-12 11:01:37 -08001877static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001878{
1879 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001880 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001881}
1882
Jan Kiszka533558b2014-01-04 18:47:20 +01001883static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1884 u32 exit_intr_info,
1885 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001886static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1887 struct vmcs12 *vmcs12,
1888 u32 reason, unsigned long qualification);
1889
Rusty Russell8b9cf982007-07-30 16:31:43 +10001890static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001891{
1892 int i;
1893
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001894 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001895 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001896 return i;
1897 return -1;
1898}
1899
Sheng Yang2384d2b2008-01-17 15:14:33 +08001900static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1901{
1902 struct {
1903 u64 vpid : 16;
1904 u64 rsvd : 48;
1905 u64 gva;
1906 } operand = { vpid, 0, gva };
1907
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001908 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001909 /* CF==1 or ZF==1 --> rc = -1 */
1910 "; ja 1f ; ud2 ; 1:"
1911 : : "a"(&operand), "c"(ext) : "cc", "memory");
1912}
1913
Sheng Yang14394422008-04-28 12:24:45 +08001914static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1915{
1916 struct {
1917 u64 eptp, gpa;
1918 } operand = {eptp, gpa};
1919
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001920 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001921 /* CF==1 or ZF==1 --> rc = -1 */
1922 "; ja 1f ; ud2 ; 1:\n"
1923 : : "a" (&operand), "c" (ext) : "cc", "memory");
1924}
1925
Avi Kivity26bb0982009-09-07 11:14:12 +03001926static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001927{
1928 int i;
1929
Rusty Russell8b9cf982007-07-30 16:31:43 +10001930 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001931 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001932 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001933 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001934}
1935
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936static void vmcs_clear(struct vmcs *vmcs)
1937{
1938 u64 phys_addr = __pa(vmcs);
1939 u8 error;
1940
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001941 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001942 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 : "cc", "memory");
1944 if (error)
1945 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1946 vmcs, phys_addr);
1947}
1948
Nadav Har'Eld462b812011-05-24 15:26:10 +03001949static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1950{
1951 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001952 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1953 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001954 loaded_vmcs->cpu = -1;
1955 loaded_vmcs->launched = 0;
1956}
1957
Dongxiao Xu7725b892010-05-11 18:29:38 +08001958static void vmcs_load(struct vmcs *vmcs)
1959{
1960 u64 phys_addr = __pa(vmcs);
1961 u8 error;
1962
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001963 if (static_branch_unlikely(&enable_evmcs))
1964 return evmcs_load(phys_addr);
1965
Dongxiao Xu7725b892010-05-11 18:29:38 +08001966 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001967 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001968 : "cc", "memory");
1969 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001970 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001971 vmcs, phys_addr);
1972}
1973
Dave Young2965faa2015-09-09 15:38:55 -07001974#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001975/*
1976 * This bitmap is used to indicate whether the vmclear
1977 * operation is enabled on all cpus. All disabled by
1978 * default.
1979 */
1980static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1981
1982static inline void crash_enable_local_vmclear(int cpu)
1983{
1984 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1985}
1986
1987static inline void crash_disable_local_vmclear(int cpu)
1988{
1989 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1990}
1991
1992static inline int crash_local_vmclear_enabled(int cpu)
1993{
1994 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1995}
1996
1997static void crash_vmclear_local_loaded_vmcss(void)
1998{
1999 int cpu = raw_smp_processor_id();
2000 struct loaded_vmcs *v;
2001
2002 if (!crash_local_vmclear_enabled(cpu))
2003 return;
2004
2005 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2006 loaded_vmcss_on_cpu_link)
2007 vmcs_clear(v->vmcs);
2008}
2009#else
2010static inline void crash_enable_local_vmclear(int cpu) { }
2011static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002012#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002013
Nadav Har'Eld462b812011-05-24 15:26:10 +03002014static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002016 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002017 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018
Nadav Har'Eld462b812011-05-24 15:26:10 +03002019 if (loaded_vmcs->cpu != cpu)
2020 return; /* vcpu migration can race with cpu offline */
2021 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002023 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002024 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002025
2026 /*
2027 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2028 * is before setting loaded_vmcs->vcpu to -1 which is done in
2029 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2030 * then adds the vmcs into percpu list before it is deleted.
2031 */
2032 smp_wmb();
2033
Nadav Har'Eld462b812011-05-24 15:26:10 +03002034 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002035 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002036}
2037
Nadav Har'Eld462b812011-05-24 15:26:10 +03002038static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002039{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002040 int cpu = loaded_vmcs->cpu;
2041
2042 if (cpu != -1)
2043 smp_call_function_single(cpu,
2044 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002045}
2046
Junaid Shahidfaff8752018-06-29 13:10:05 -07002047static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2048{
2049 if (vpid == 0)
2050 return true;
2051
2052 if (cpu_has_vmx_invvpid_individual_addr()) {
2053 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2054 return true;
2055 }
2056
2057 return false;
2058}
2059
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002060static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002061{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002062 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002063 return;
2064
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002065 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002066 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002067}
2068
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002069static inline void vpid_sync_vcpu_global(void)
2070{
2071 if (cpu_has_vmx_invvpid_global())
2072 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2073}
2074
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002075static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002076{
2077 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002078 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002079 else
2080 vpid_sync_vcpu_global();
2081}
2082
Sheng Yang14394422008-04-28 12:24:45 +08002083static inline void ept_sync_global(void)
2084{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002085 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002086}
2087
2088static inline void ept_sync_context(u64 eptp)
2089{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002090 if (cpu_has_vmx_invept_context())
2091 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2092 else
2093 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002094}
2095
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002096static __always_inline void vmcs_check16(unsigned long field)
2097{
2098 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2099 "16-bit accessor invalid for 64-bit field");
2100 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2101 "16-bit accessor invalid for 64-bit high field");
2102 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2103 "16-bit accessor invalid for 32-bit high field");
2104 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2105 "16-bit accessor invalid for natural width field");
2106}
2107
2108static __always_inline void vmcs_check32(unsigned long field)
2109{
2110 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2111 "32-bit accessor invalid for 16-bit field");
2112 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2113 "32-bit accessor invalid for natural width field");
2114}
2115
2116static __always_inline void vmcs_check64(unsigned long field)
2117{
2118 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2119 "64-bit accessor invalid for 16-bit field");
2120 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2121 "64-bit accessor invalid for 64-bit high field");
2122 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2123 "64-bit accessor invalid for 32-bit field");
2124 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2125 "64-bit accessor invalid for natural width field");
2126}
2127
2128static __always_inline void vmcs_checkl(unsigned long field)
2129{
2130 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2131 "Natural width accessor invalid for 16-bit field");
2132 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2133 "Natural width accessor invalid for 64-bit field");
2134 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2135 "Natural width accessor invalid for 64-bit high field");
2136 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2137 "Natural width accessor invalid for 32-bit field");
2138}
2139
2140static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141{
Avi Kivity5e520e62011-05-15 10:13:12 -04002142 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143
Avi Kivity5e520e62011-05-15 10:13:12 -04002144 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2145 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146 return value;
2147}
2148
Avi Kivity96304212011-05-15 10:13:13 -04002149static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002151 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002152 if (static_branch_unlikely(&enable_evmcs))
2153 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002154 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155}
2156
Avi Kivity96304212011-05-15 10:13:13 -04002157static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002159 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002160 if (static_branch_unlikely(&enable_evmcs))
2161 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002162 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163}
2164
Avi Kivity96304212011-05-15 10:13:13 -04002165static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002167 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002168 if (static_branch_unlikely(&enable_evmcs))
2169 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002170#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002171 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002173 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174#endif
2175}
2176
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002177static __always_inline unsigned long vmcs_readl(unsigned long field)
2178{
2179 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002180 if (static_branch_unlikely(&enable_evmcs))
2181 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002182 return __vmcs_readl(field);
2183}
2184
Avi Kivitye52de1b2007-01-05 16:36:56 -08002185static noinline void vmwrite_error(unsigned long field, unsigned long value)
2186{
2187 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2188 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2189 dump_stack();
2190}
2191
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002192static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193{
2194 u8 error;
2195
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002196 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002197 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002198 if (unlikely(error))
2199 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200}
2201
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002202static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002204 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002205 if (static_branch_unlikely(&enable_evmcs))
2206 return evmcs_write16(field, value);
2207
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002208 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209}
2210
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002211static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002213 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002214 if (static_branch_unlikely(&enable_evmcs))
2215 return evmcs_write32(field, value);
2216
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002217 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218}
2219
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002220static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002222 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002223 if (static_branch_unlikely(&enable_evmcs))
2224 return evmcs_write64(field, value);
2225
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002226 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002227#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002229 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230#endif
2231}
2232
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002233static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002234{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002235 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002236 if (static_branch_unlikely(&enable_evmcs))
2237 return evmcs_write64(field, value);
2238
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002239 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002240}
2241
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002242static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002243{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002244 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2245 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002246 if (static_branch_unlikely(&enable_evmcs))
2247 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2248
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002249 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2250}
2251
2252static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2253{
2254 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2255 "vmcs_set_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);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002260}
2261
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002262static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2263{
2264 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2265}
2266
Gleb Natapov2961e8762013-11-25 15:37:13 +02002267static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2268{
2269 vmcs_write32(VM_ENTRY_CONTROLS, val);
2270 vmx->vm_entry_controls_shadow = val;
2271}
2272
2273static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2274{
2275 if (vmx->vm_entry_controls_shadow != val)
2276 vm_entry_controls_init(vmx, val);
2277}
2278
2279static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2280{
2281 return vmx->vm_entry_controls_shadow;
2282}
2283
2284
2285static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2286{
2287 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2288}
2289
2290static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2291{
2292 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2293}
2294
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002295static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2296{
2297 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2298}
2299
Gleb Natapov2961e8762013-11-25 15:37:13 +02002300static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2301{
2302 vmcs_write32(VM_EXIT_CONTROLS, val);
2303 vmx->vm_exit_controls_shadow = val;
2304}
2305
2306static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2307{
2308 if (vmx->vm_exit_controls_shadow != val)
2309 vm_exit_controls_init(vmx, val);
2310}
2311
2312static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2313{
2314 return vmx->vm_exit_controls_shadow;
2315}
2316
2317
2318static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2319{
2320 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2321}
2322
2323static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2324{
2325 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2326}
2327
Avi Kivity2fb92db2011-04-27 19:42:18 +03002328static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2329{
2330 vmx->segment_cache.bitmask = 0;
2331}
2332
2333static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2334 unsigned field)
2335{
2336 bool ret;
2337 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2338
2339 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2340 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2341 vmx->segment_cache.bitmask = 0;
2342 }
2343 ret = vmx->segment_cache.bitmask & mask;
2344 vmx->segment_cache.bitmask |= mask;
2345 return ret;
2346}
2347
2348static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2349{
2350 u16 *p = &vmx->segment_cache.seg[seg].selector;
2351
2352 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2353 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2354 return *p;
2355}
2356
2357static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2358{
2359 ulong *p = &vmx->segment_cache.seg[seg].base;
2360
2361 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2362 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2363 return *p;
2364}
2365
2366static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2367{
2368 u32 *p = &vmx->segment_cache.seg[seg].limit;
2369
2370 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2371 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2372 return *p;
2373}
2374
2375static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2376{
2377 u32 *p = &vmx->segment_cache.seg[seg].ar;
2378
2379 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2380 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2381 return *p;
2382}
2383
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002384static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2385{
2386 u32 eb;
2387
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002388 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002389 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002390 /*
2391 * Guest access to VMware backdoor ports could legitimately
2392 * trigger #GP because of TSS I/O permission bitmap.
2393 * We intercept those #GP and allow access to them anyway
2394 * as VMware does.
2395 */
2396 if (enable_vmware_backdoor)
2397 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002398 if ((vcpu->guest_debug &
2399 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2400 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2401 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002402 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002403 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002404 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002405 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002406
2407 /* When we are running a nested L2 guest and L1 specified for it a
2408 * certain exception bitmap, we must trap the same exceptions and pass
2409 * them to L1. When running L2, we will only handle the exceptions
2410 * specified above if L1 did not want them.
2411 */
2412 if (is_guest_mode(vcpu))
2413 eb |= get_vmcs12(vcpu)->exception_bitmap;
2414
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002415 vmcs_write32(EXCEPTION_BITMAP, eb);
2416}
2417
Ashok Raj15d45072018-02-01 22:59:43 +01002418/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002419 * Check if MSR is intercepted for currently loaded MSR bitmap.
2420 */
2421static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2422{
2423 unsigned long *msr_bitmap;
2424 int f = sizeof(unsigned long);
2425
2426 if (!cpu_has_vmx_msr_bitmap())
2427 return true;
2428
2429 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2430
2431 if (msr <= 0x1fff) {
2432 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2433 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2434 msr &= 0x1fff;
2435 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2436 }
2437
2438 return true;
2439}
2440
2441/*
Ashok Raj15d45072018-02-01 22:59:43 +01002442 * Check if MSR is intercepted for L01 MSR bitmap.
2443 */
2444static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2445{
2446 unsigned long *msr_bitmap;
2447 int f = sizeof(unsigned long);
2448
2449 if (!cpu_has_vmx_msr_bitmap())
2450 return true;
2451
2452 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2453
2454 if (msr <= 0x1fff) {
2455 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2456 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2457 msr &= 0x1fff;
2458 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2459 }
2460
2461 return true;
2462}
2463
Gleb Natapov2961e8762013-11-25 15:37:13 +02002464static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2465 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002466{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002467 vm_entry_controls_clearbit(vmx, entry);
2468 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002469}
2470
Avi Kivity61d2ef22010-04-28 16:40:38 +03002471static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2472{
2473 unsigned i;
2474 struct msr_autoload *m = &vmx->msr_autoload;
2475
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002476 switch (msr) {
2477 case MSR_EFER:
2478 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002479 clear_atomic_switch_msr_special(vmx,
2480 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002481 VM_EXIT_LOAD_IA32_EFER);
2482 return;
2483 }
2484 break;
2485 case MSR_CORE_PERF_GLOBAL_CTRL:
2486 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002487 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002488 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2489 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2490 return;
2491 }
2492 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002493 }
2494
Avi Kivity61d2ef22010-04-28 16:40:38 +03002495 for (i = 0; i < m->nr; ++i)
2496 if (m->guest[i].index == msr)
2497 break;
2498
2499 if (i == m->nr)
2500 return;
2501 --m->nr;
2502 m->guest[i] = m->guest[m->nr];
2503 m->host[i] = m->host[m->nr];
2504 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2505 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2506}
2507
Gleb Natapov2961e8762013-11-25 15:37:13 +02002508static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2509 unsigned long entry, unsigned long exit,
2510 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2511 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002512{
2513 vmcs_write64(guest_val_vmcs, guest_val);
2514 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002515 vm_entry_controls_setbit(vmx, entry);
2516 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002517}
2518
Avi Kivity61d2ef22010-04-28 16:40:38 +03002519static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2520 u64 guest_val, u64 host_val)
2521{
2522 unsigned i;
2523 struct msr_autoload *m = &vmx->msr_autoload;
2524
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002525 switch (msr) {
2526 case MSR_EFER:
2527 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002528 add_atomic_switch_msr_special(vmx,
2529 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002530 VM_EXIT_LOAD_IA32_EFER,
2531 GUEST_IA32_EFER,
2532 HOST_IA32_EFER,
2533 guest_val, host_val);
2534 return;
2535 }
2536 break;
2537 case MSR_CORE_PERF_GLOBAL_CTRL:
2538 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002539 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002540 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2541 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2542 GUEST_IA32_PERF_GLOBAL_CTRL,
2543 HOST_IA32_PERF_GLOBAL_CTRL,
2544 guest_val, host_val);
2545 return;
2546 }
2547 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002548 case MSR_IA32_PEBS_ENABLE:
2549 /* PEBS needs a quiescent period after being disabled (to write
2550 * a record). Disabling PEBS through VMX MSR swapping doesn't
2551 * provide that period, so a CPU could write host's record into
2552 * guest's memory.
2553 */
2554 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002555 }
2556
Avi Kivity61d2ef22010-04-28 16:40:38 +03002557 for (i = 0; i < m->nr; ++i)
2558 if (m->guest[i].index == msr)
2559 break;
2560
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002561 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002562 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002563 "Can't add msr %x\n", msr);
2564 return;
2565 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002566 ++m->nr;
2567 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2568 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2569 }
2570
2571 m->guest[i].index = msr;
2572 m->guest[i].value = guest_val;
2573 m->host[i].index = msr;
2574 m->host[i].value = host_val;
2575}
2576
Avi Kivity92c0d902009-10-29 11:00:16 +02002577static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002578{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002579 u64 guest_efer = vmx->vcpu.arch.efer;
2580 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002581
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002582 if (!enable_ept) {
2583 /*
2584 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2585 * host CPUID is more efficient than testing guest CPUID
2586 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2587 */
2588 if (boot_cpu_has(X86_FEATURE_SMEP))
2589 guest_efer |= EFER_NX;
2590 else if (!(guest_efer & EFER_NX))
2591 ignore_bits |= EFER_NX;
2592 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002593
Avi Kivity51c6cf62007-08-29 03:48:05 +03002594 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002595 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002596 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002597 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002598#ifdef CONFIG_X86_64
2599 ignore_bits |= EFER_LMA | EFER_LME;
2600 /* SCE is meaningful only in long mode on Intel */
2601 if (guest_efer & EFER_LMA)
2602 ignore_bits &= ~(u64)EFER_SCE;
2603#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002604
2605 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002606
2607 /*
2608 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2609 * On CPUs that support "load IA32_EFER", always switch EFER
2610 * atomically, since it's faster than switching it manually.
2611 */
2612 if (cpu_has_load_ia32_efer ||
2613 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002614 if (!(guest_efer & EFER_LMA))
2615 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002616 if (guest_efer != host_efer)
2617 add_atomic_switch_msr(vmx, MSR_EFER,
2618 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002619 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002620 } else {
2621 guest_efer &= ~ignore_bits;
2622 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002623
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002624 vmx->guest_msrs[efer_offset].data = guest_efer;
2625 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2626
2627 return true;
2628 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002629}
2630
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002631#ifdef CONFIG_X86_32
2632/*
2633 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2634 * VMCS rather than the segment table. KVM uses this helper to figure
2635 * out the current bases to poke them into the VMCS before entry.
2636 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002637static unsigned long segment_base(u16 selector)
2638{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002639 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002640 unsigned long v;
2641
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002642 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002643 return 0;
2644
Thomas Garnier45fc8752017-03-14 10:05:08 -07002645 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002646
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002647 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002648 u16 ldt_selector = kvm_read_ldt();
2649
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002650 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002651 return 0;
2652
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002653 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002654 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002655 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002656 return v;
2657}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002658#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002659
Avi Kivity04d2cc72007-09-10 18:10:54 +03002660static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002661{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002662 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002663#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002664 int cpu = raw_smp_processor_id();
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002665 unsigned long fs_base, kernel_gs_base;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002666#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002667 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002668
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002669 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002670 return;
2671
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002672 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002673 /*
2674 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2675 * allow segment selectors with cpl > 0 or ti == 1.
2676 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002677 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002678 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002679
2680#ifdef CONFIG_X86_64
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002681 if (likely(is_64bit_mm(current->mm))) {
2682 save_fsgs_for_kvm();
2683 vmx->host_state.fs_sel = current->thread.fsindex;
2684 vmx->host_state.gs_sel = current->thread.gsindex;
2685 fs_base = current->thread.fsbase;
2686 kernel_gs_base = current->thread.gsbase;
2687 } else {
2688#endif
2689 savesegment(fs, vmx->host_state.fs_sel);
2690 savesegment(gs, vmx->host_state.gs_sel);
2691#ifdef CONFIG_X86_64
2692 fs_base = read_msr(MSR_FS_BASE);
2693 kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2694 }
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002695#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002696 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002697 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002698 vmx->host_state.fs_reload_needed = 0;
2699 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002700 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002701 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002702 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002703 if (!(vmx->host_state.gs_sel & 7))
2704 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002705 else {
2706 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002707 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002708 }
2709
2710#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002711 savesegment(ds, vmx->host_state.ds_sel);
2712 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002713
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002714 vmcs_writel(HOST_FS_BASE, fs_base);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002715 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002716
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002717 vmx->msr_host_kernel_gs_base = kernel_gs_base;
Avi Kivityc8770e72010-11-11 12:37:26 +02002718 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002719 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002720#else
2721 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2722 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2723#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002724 for (i = 0; i < vmx->save_nmsrs; ++i)
2725 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002726 vmx->guest_msrs[i].data,
2727 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002728}
2729
Avi Kivitya9b21b62008-06-24 11:48:49 +03002730static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002731{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002732 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002733 return;
2734
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002735 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002736 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002737#ifdef CONFIG_X86_64
2738 if (is_long_mode(&vmx->vcpu))
2739 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2740#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002741 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002742 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002743#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002744 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002745#else
2746 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002747#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002748 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002749 if (vmx->host_state.fs_reload_needed)
2750 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002751#ifdef CONFIG_X86_64
2752 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2753 loadsegment(ds, vmx->host_state.ds_sel);
2754 loadsegment(es, vmx->host_state.es_sel);
2755 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002756#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002757 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002758#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002759 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002760#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002761 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002762}
2763
Avi Kivitya9b21b62008-06-24 11:48:49 +03002764static void vmx_load_host_state(struct vcpu_vmx *vmx)
2765{
2766 preempt_disable();
2767 __vmx_load_host_state(vmx);
2768 preempt_enable();
2769}
2770
Feng Wu28b835d2015-09-18 22:29:54 +08002771static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2772{
2773 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2774 struct pi_desc old, new;
2775 unsigned int dest;
2776
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002777 /*
2778 * In case of hot-plug or hot-unplug, we may have to undo
2779 * vmx_vcpu_pi_put even if there is no assigned device. And we
2780 * always keep PI.NDST up to date for simplicity: it makes the
2781 * code easier, and CPU migration is not a fast path.
2782 */
2783 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002784 return;
2785
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002786 /*
2787 * First handle the simple case where no cmpxchg is necessary; just
2788 * allow posting non-urgent interrupts.
2789 *
2790 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2791 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2792 * expects the VCPU to be on the blocked_vcpu_list that matches
2793 * PI.NDST.
2794 */
2795 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2796 vcpu->cpu == cpu) {
2797 pi_clear_sn(pi_desc);
2798 return;
2799 }
2800
2801 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002802 do {
2803 old.control = new.control = pi_desc->control;
2804
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002805 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002806
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002807 if (x2apic_enabled())
2808 new.ndst = dest;
2809 else
2810 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002811
Feng Wu28b835d2015-09-18 22:29:54 +08002812 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002813 } while (cmpxchg64(&pi_desc->control, old.control,
2814 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002815}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002816
Peter Feinerc95ba922016-08-17 09:36:47 -07002817static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2818{
2819 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2820 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2821}
2822
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823/*
2824 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2825 * vcpu mutex is already taken.
2826 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002827static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002829 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002830 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002832 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002833 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002834 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002835 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002836
2837 /*
2838 * Read loaded_vmcs->cpu should be before fetching
2839 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2840 * See the comments in __loaded_vmcs_clear().
2841 */
2842 smp_rmb();
2843
Nadav Har'Eld462b812011-05-24 15:26:10 +03002844 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2845 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002846 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002847 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002848 }
2849
2850 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2851 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2852 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002853 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002854 }
2855
2856 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002857 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002858 unsigned long sysenter_esp;
2859
2860 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002861
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862 /*
2863 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002864 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002866 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002867 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002868 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002870 /*
2871 * VM exits change the host TR limit to 0x67 after a VM
2872 * exit. This is okay, since 0x67 covers everything except
2873 * the IO bitmap and have have code to handle the IO bitmap
2874 * being lost after a VM exit.
2875 */
2876 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2877
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2879 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002880
Nadav Har'Eld462b812011-05-24 15:26:10 +03002881 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 }
Feng Wu28b835d2015-09-18 22:29:54 +08002883
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002884 /* Setup TSC multiplier */
2885 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002886 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2887 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002888
Feng Wu28b835d2015-09-18 22:29:54 +08002889 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002890 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002891 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002892}
2893
2894static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2895{
2896 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2897
2898 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002899 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2900 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002901 return;
2902
2903 /* Set SN when the vCPU is preempted */
2904 if (vcpu->preempted)
2905 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906}
2907
2908static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2909{
Feng Wu28b835d2015-09-18 22:29:54 +08002910 vmx_vcpu_pi_put(vcpu);
2911
Avi Kivitya9b21b62008-06-24 11:48:49 +03002912 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913}
2914
Wanpeng Lif244dee2017-07-20 01:11:54 -07002915static bool emulation_required(struct kvm_vcpu *vcpu)
2916{
2917 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2918}
2919
Avi Kivityedcafe32009-12-30 18:07:40 +02002920static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2921
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002922/*
2923 * Return the cr0 value that a nested guest would read. This is a combination
2924 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2925 * its hypervisor (cr0_read_shadow).
2926 */
2927static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2928{
2929 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2930 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2931}
2932static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2933{
2934 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2935 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2936}
2937
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2939{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002940 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002941
Avi Kivity6de12732011-03-07 12:51:22 +02002942 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2943 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2944 rflags = vmcs_readl(GUEST_RFLAGS);
2945 if (to_vmx(vcpu)->rmode.vm86_active) {
2946 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2947 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2948 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2949 }
2950 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002951 }
Avi Kivity6de12732011-03-07 12:51:22 +02002952 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002953}
2954
2955static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2956{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002957 unsigned long old_rflags = vmx_get_rflags(vcpu);
2958
Avi Kivity6de12732011-03-07 12:51:22 +02002959 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2960 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002961 if (to_vmx(vcpu)->rmode.vm86_active) {
2962 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002963 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002964 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002966
2967 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2968 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969}
2970
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002971static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002972{
2973 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2974 int ret = 0;
2975
2976 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002977 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002978 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002979 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002980
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002981 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002982}
2983
2984static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2985{
2986 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2987 u32 interruptibility = interruptibility_old;
2988
2989 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2990
Jan Kiszka48005f62010-02-19 19:38:07 +01002991 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002992 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002993 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002994 interruptibility |= GUEST_INTR_STATE_STI;
2995
2996 if ((interruptibility != interruptibility_old))
2997 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2998}
2999
Avi Kivity6aa8b732006-12-10 02:21:36 -08003000static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3001{
3002 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003003
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003004 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003006 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007
Glauber Costa2809f5d2009-05-12 16:21:05 -04003008 /* skipping an emulated instruction also counts */
3009 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010}
3011
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003012static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3013 unsigned long exit_qual)
3014{
3015 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3016 unsigned int nr = vcpu->arch.exception.nr;
3017 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3018
3019 if (vcpu->arch.exception.has_error_code) {
3020 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3021 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3022 }
3023
3024 if (kvm_exception_is_soft(nr))
3025 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3026 else
3027 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3028
3029 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3030 vmx_get_nmi_mask(vcpu))
3031 intr_info |= INTR_INFO_UNBLOCK_NMI;
3032
3033 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3034}
3035
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003036/*
3037 * KVM wants to inject page-faults which it got to the guest. This function
3038 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003039 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003040static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003041{
3042 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003043 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003044
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003045 if (nr == PF_VECTOR) {
3046 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003047 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003048 return 1;
3049 }
3050 /*
3051 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3052 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3053 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3054 * can be written only when inject_pending_event runs. This should be
3055 * conditional on a new capability---if the capability is disabled,
3056 * kvm_multiple_exception would write the ancillary information to
3057 * CR2 or DR6, for backwards ABI-compatibility.
3058 */
3059 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3060 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003061 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003062 return 1;
3063 }
3064 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003065 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003066 if (nr == DB_VECTOR)
3067 *exit_qual = vcpu->arch.dr6;
3068 else
3069 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003070 return 1;
3071 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003072 }
3073
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003074 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003075}
3076
Wanpeng Licaa057a2018-03-12 04:53:03 -07003077static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3078{
3079 /*
3080 * Ensure that we clear the HLT state in the VMCS. We don't need to
3081 * explicitly skip the instruction because if the HLT state is set,
3082 * then the instruction is already executing and RIP has already been
3083 * advanced.
3084 */
3085 if (kvm_hlt_in_guest(vcpu->kvm) &&
3086 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3087 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3088}
3089
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003090static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003091{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003092 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003093 unsigned nr = vcpu->arch.exception.nr;
3094 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003095 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003096 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003097
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003098 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003099 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003100 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3101 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003102
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003103 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003104 int inc_eip = 0;
3105 if (kvm_exception_is_soft(nr))
3106 inc_eip = vcpu->arch.event_exit_inst_len;
3107 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003108 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003109 return;
3110 }
3111
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003112 WARN_ON_ONCE(vmx->emulation_required);
3113
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003114 if (kvm_exception_is_soft(nr)) {
3115 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3116 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003117 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3118 } else
3119 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3120
3121 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003122
3123 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003124}
3125
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003126static bool vmx_rdtscp_supported(void)
3127{
3128 return cpu_has_vmx_rdtscp();
3129}
3130
Mao, Junjiead756a12012-07-02 01:18:48 +00003131static bool vmx_invpcid_supported(void)
3132{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003133 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003134}
3135
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136/*
Eddie Donga75beee2007-05-17 18:55:15 +03003137 * Swap MSR entry in host/guest MSR entry array.
3138 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003139static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003140{
Avi Kivity26bb0982009-09-07 11:14:12 +03003141 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003142
3143 tmp = vmx->guest_msrs[to];
3144 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3145 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003146}
3147
3148/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003149 * Set up the vmcs to automatically save and restore system
3150 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3151 * mode, as fiddling with msrs is very expensive.
3152 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003153static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003154{
Avi Kivity26bb0982009-09-07 11:14:12 +03003155 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003156
Eddie Donga75beee2007-05-17 18:55:15 +03003157 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003158#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003159 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003160 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003161 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003162 move_msr_up(vmx, index, save_nmsrs++);
3163 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003164 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003165 move_msr_up(vmx, index, save_nmsrs++);
3166 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003167 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003168 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003169 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003170 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003171 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003172 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003173 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003174 * if efer.sce is enabled.
3175 */
Brian Gerst8c065852010-07-17 09:03:26 -04003176 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003177 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003178 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003179 }
Eddie Donga75beee2007-05-17 18:55:15 +03003180#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003181 index = __find_msr_index(vmx, MSR_EFER);
3182 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003183 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003184
Avi Kivity26bb0982009-09-07 11:14:12 +03003185 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003186
Yang Zhang8d146952013-01-25 10:18:50 +08003187 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003188 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003189}
3190
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003191static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003193 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003195 if (is_guest_mode(vcpu) &&
3196 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3197 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3198
3199 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200}
3201
3202/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003203 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003205static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003207 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003208 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003209 * We're here if L1 chose not to trap WRMSR to TSC. According
3210 * to the spec, this should set L1's TSC; The offset that L1
3211 * set for L2 remains unchanged, and still needs to be added
3212 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003213 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003214 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003215 /* recalculate vmcs02.TSC_OFFSET: */
3216 vmcs12 = get_vmcs12(vcpu);
3217 vmcs_write64(TSC_OFFSET, offset +
3218 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3219 vmcs12->tsc_offset : 0));
3220 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003221 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3222 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003223 vmcs_write64(TSC_OFFSET, offset);
3224 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225}
3226
Nadav Har'El801d3422011-05-25 23:02:23 +03003227/*
3228 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3229 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3230 * all guests if the "nested" module option is off, and can also be disabled
3231 * for a single guest by disabling its VMX cpuid bit.
3232 */
3233static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3234{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003235 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003236}
3237
Avi Kivity6aa8b732006-12-10 02:21:36 -08003238/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003239 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3240 * returned for the various VMX controls MSRs when nested VMX is enabled.
3241 * The same values should also be used to verify that vmcs12 control fields are
3242 * valid during nested entry from L1 to L2.
3243 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3244 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3245 * bit in the high half is on if the corresponding bit in the control field
3246 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003247 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003248static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003249{
Paolo Bonzini13893092018-02-26 13:40:09 +01003250 if (!nested) {
3251 memset(msrs, 0, sizeof(*msrs));
3252 return;
3253 }
3254
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003255 /*
3256 * Note that as a general rule, the high half of the MSRs (bits in
3257 * the control fields which may be 1) should be initialized by the
3258 * intersection of the underlying hardware's MSR (i.e., features which
3259 * can be supported) and the list of features we want to expose -
3260 * because they are known to be properly supported in our code.
3261 * Also, usually, the low half of the MSRs (bits which must be 1) can
3262 * be set to 0, meaning that L1 may turn off any of these bits. The
3263 * reason is that if one of these bits is necessary, it will appear
3264 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3265 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003266 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003267 * These rules have exceptions below.
3268 */
3269
3270 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003271 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003272 msrs->pinbased_ctls_low,
3273 msrs->pinbased_ctls_high);
3274 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003275 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003276 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003277 PIN_BASED_EXT_INTR_MASK |
3278 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003279 PIN_BASED_VIRTUAL_NMIS |
3280 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003281 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003282 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003283 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003284
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003285 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003286 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003287 msrs->exit_ctls_low,
3288 msrs->exit_ctls_high);
3289 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003290 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003291
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003292 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003293#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003294 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003295#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003296 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003297 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003298 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003299 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003300 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3301
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003302 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003303 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003304
Jan Kiszka2996fca2014-06-16 13:59:43 +02003305 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003306 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003307
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003308 /* entry controls */
3309 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003310 msrs->entry_ctls_low,
3311 msrs->entry_ctls_high);
3312 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003313 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003314 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003315#ifdef CONFIG_X86_64
3316 VM_ENTRY_IA32E_MODE |
3317#endif
3318 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003319 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003320 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003321 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003322 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003323
Jan Kiszka2996fca2014-06-16 13:59:43 +02003324 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003325 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003326
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003327 /* cpu-based controls */
3328 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003329 msrs->procbased_ctls_low,
3330 msrs->procbased_ctls_high);
3331 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003332 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003333 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003334 CPU_BASED_VIRTUAL_INTR_PENDING |
3335 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003336 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3337 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3338 CPU_BASED_CR3_STORE_EXITING |
3339#ifdef CONFIG_X86_64
3340 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3341#endif
3342 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003343 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3344 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3345 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3346 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003347 /*
3348 * We can allow some features even when not supported by the
3349 * hardware. For example, L1 can specify an MSR bitmap - and we
3350 * can use it to avoid exits to L1 - even when L0 runs L2
3351 * without MSR bitmaps.
3352 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003353 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003354 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003355 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003356
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003357 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003358 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003359 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3360
Paolo Bonzini80154d72017-08-24 13:55:35 +02003361 /*
3362 * secondary cpu-based controls. Do not include those that
3363 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3364 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003365 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003366 msrs->secondary_ctls_low,
3367 msrs->secondary_ctls_high);
3368 msrs->secondary_ctls_low = 0;
3369 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003370 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003371 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003372 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003373 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003374 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003375 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003376 /*
3377 * We can emulate "VMCS shadowing," even if the hardware
3378 * doesn't support it.
3379 */
3380 msrs->secondary_ctls_high |=
3381 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003382
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003383 if (enable_ept) {
3384 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003385 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003386 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003387 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003388 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003389 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003390 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003391 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003392 msrs->ept_caps &= vmx_capability.ept;
3393 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003394 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3395 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003396 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003397 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003398 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003399 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003400 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003401 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003402
Bandan Das27c42a12017-08-03 15:54:42 -04003403 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003404 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003405 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003406 /*
3407 * Advertise EPTP switching unconditionally
3408 * since we emulate it
3409 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003410 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003411 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003412 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003413 }
3414
Paolo Bonzinief697a72016-03-18 16:58:38 +01003415 /*
3416 * Old versions of KVM use the single-context version without
3417 * checking for support, so declare that it is supported even
3418 * though it is treated as global context. The alternative is
3419 * not failing the single-context invvpid, and it is worse.
3420 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003421 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003422 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003423 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003424 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003425 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003426 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003427
Radim Krčmář0790ec12015-03-17 14:02:32 +01003428 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003429 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003430 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3431
Jan Kiszkac18911a2013-03-13 16:06:41 +01003432 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003433 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003434 msrs->misc_low,
3435 msrs->misc_high);
3436 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3437 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003438 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003439 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003440 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003441 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003442
3443 /*
3444 * This MSR reports some information about VMX support. We
3445 * should return information about the VMX we emulate for the
3446 * guest, and the VMCS structure we give it - not about the
3447 * VMX support of the underlying hardware.
3448 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003449 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003450 VMCS12_REVISION |
3451 VMX_BASIC_TRUE_CTLS |
3452 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3453 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3454
3455 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003456 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003457
3458 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003459 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003460 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3461 * We picked the standard core2 setting.
3462 */
3463#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3464#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003465 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3466 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003467
3468 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003469 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3470 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003471
3472 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003473 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003474}
3475
David Matlack38991522016-11-29 18:14:08 -08003476/*
3477 * if fixed0[i] == 1: val[i] must be 1
3478 * if fixed1[i] == 0: val[i] must be 0
3479 */
3480static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3481{
3482 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003483}
3484
3485static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3486{
David Matlack38991522016-11-29 18:14:08 -08003487 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003488}
3489
3490static inline u64 vmx_control_msr(u32 low, u32 high)
3491{
3492 return low | ((u64)high << 32);
3493}
3494
David Matlack62cc6b9d2016-11-29 18:14:07 -08003495static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3496{
3497 superset &= mask;
3498 subset &= mask;
3499
3500 return (superset | subset) == superset;
3501}
3502
3503static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3504{
3505 const u64 feature_and_reserved =
3506 /* feature (except bit 48; see below) */
3507 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3508 /* reserved */
3509 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003510 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003511
3512 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3513 return -EINVAL;
3514
3515 /*
3516 * KVM does not emulate a version of VMX that constrains physical
3517 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3518 */
3519 if (data & BIT_ULL(48))
3520 return -EINVAL;
3521
3522 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3523 vmx_basic_vmcs_revision_id(data))
3524 return -EINVAL;
3525
3526 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3527 return -EINVAL;
3528
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003529 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003530 return 0;
3531}
3532
3533static int
3534vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3535{
3536 u64 supported;
3537 u32 *lowp, *highp;
3538
3539 switch (msr_index) {
3540 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003541 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3542 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003543 break;
3544 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003545 lowp = &vmx->nested.msrs.procbased_ctls_low;
3546 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003547 break;
3548 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003549 lowp = &vmx->nested.msrs.exit_ctls_low;
3550 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003551 break;
3552 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003553 lowp = &vmx->nested.msrs.entry_ctls_low;
3554 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003555 break;
3556 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003557 lowp = &vmx->nested.msrs.secondary_ctls_low;
3558 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003559 break;
3560 default:
3561 BUG();
3562 }
3563
3564 supported = vmx_control_msr(*lowp, *highp);
3565
3566 /* Check must-be-1 bits are still 1. */
3567 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3568 return -EINVAL;
3569
3570 /* Check must-be-0 bits are still 0. */
3571 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3572 return -EINVAL;
3573
3574 *lowp = data;
3575 *highp = data >> 32;
3576 return 0;
3577}
3578
3579static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3580{
3581 const u64 feature_and_reserved_bits =
3582 /* feature */
3583 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3584 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3585 /* reserved */
3586 GENMASK_ULL(13, 9) | BIT_ULL(31);
3587 u64 vmx_misc;
3588
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003589 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3590 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003591
3592 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3593 return -EINVAL;
3594
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003595 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003596 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3597 vmx_misc_preemption_timer_rate(data) !=
3598 vmx_misc_preemption_timer_rate(vmx_misc))
3599 return -EINVAL;
3600
3601 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3602 return -EINVAL;
3603
3604 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3605 return -EINVAL;
3606
3607 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3608 return -EINVAL;
3609
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003610 vmx->nested.msrs.misc_low = data;
3611 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003612
3613 /*
3614 * If L1 has read-only VM-exit information fields, use the
3615 * less permissive vmx_vmwrite_bitmap to specify write
3616 * permissions for the shadow VMCS.
3617 */
3618 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3619 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3620
David Matlack62cc6b9d2016-11-29 18:14:07 -08003621 return 0;
3622}
3623
3624static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3625{
3626 u64 vmx_ept_vpid_cap;
3627
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003628 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3629 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003630
3631 /* Every bit is either reserved or a feature bit. */
3632 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3633 return -EINVAL;
3634
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003635 vmx->nested.msrs.ept_caps = data;
3636 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003637 return 0;
3638}
3639
3640static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3641{
3642 u64 *msr;
3643
3644 switch (msr_index) {
3645 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003646 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003647 break;
3648 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003650 break;
3651 default:
3652 BUG();
3653 }
3654
3655 /*
3656 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3657 * must be 1 in the restored value.
3658 */
3659 if (!is_bitwise_subset(data, *msr, -1ULL))
3660 return -EINVAL;
3661
3662 *msr = data;
3663 return 0;
3664}
3665
3666/*
3667 * Called when userspace is restoring VMX MSRs.
3668 *
3669 * Returns 0 on success, non-0 otherwise.
3670 */
3671static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3672{
3673 struct vcpu_vmx *vmx = to_vmx(vcpu);
3674
Jim Mattsona943ac52018-05-29 09:11:32 -07003675 /*
3676 * Don't allow changes to the VMX capability MSRs while the vCPU
3677 * is in VMX operation.
3678 */
3679 if (vmx->nested.vmxon)
3680 return -EBUSY;
3681
David Matlack62cc6b9d2016-11-29 18:14:07 -08003682 switch (msr_index) {
3683 case MSR_IA32_VMX_BASIC:
3684 return vmx_restore_vmx_basic(vmx, data);
3685 case MSR_IA32_VMX_PINBASED_CTLS:
3686 case MSR_IA32_VMX_PROCBASED_CTLS:
3687 case MSR_IA32_VMX_EXIT_CTLS:
3688 case MSR_IA32_VMX_ENTRY_CTLS:
3689 /*
3690 * The "non-true" VMX capability MSRs are generated from the
3691 * "true" MSRs, so we do not support restoring them directly.
3692 *
3693 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3694 * should restore the "true" MSRs with the must-be-1 bits
3695 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3696 * DEFAULT SETTINGS".
3697 */
3698 return -EINVAL;
3699 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3700 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3701 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3702 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3703 case MSR_IA32_VMX_PROCBASED_CTLS2:
3704 return vmx_restore_control_msr(vmx, msr_index, data);
3705 case MSR_IA32_VMX_MISC:
3706 return vmx_restore_vmx_misc(vmx, data);
3707 case MSR_IA32_VMX_CR0_FIXED0:
3708 case MSR_IA32_VMX_CR4_FIXED0:
3709 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3710 case MSR_IA32_VMX_CR0_FIXED1:
3711 case MSR_IA32_VMX_CR4_FIXED1:
3712 /*
3713 * These MSRs are generated based on the vCPU's CPUID, so we
3714 * do not support restoring them directly.
3715 */
3716 return -EINVAL;
3717 case MSR_IA32_VMX_EPT_VPID_CAP:
3718 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3719 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003720 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003721 return 0;
3722 default:
3723 /*
3724 * The rest of the VMX capability MSRs do not support restore.
3725 */
3726 return -EINVAL;
3727 }
3728}
3729
Jan Kiszkacae50132014-01-04 18:47:22 +01003730/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003731static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003732{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003733 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003734 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003735 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003736 break;
3737 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3738 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003739 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003740 msrs->pinbased_ctls_low,
3741 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003742 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3743 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003744 break;
3745 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3746 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003747 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003748 msrs->procbased_ctls_low,
3749 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003750 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3751 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003752 break;
3753 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3754 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003755 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003756 msrs->exit_ctls_low,
3757 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003758 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3759 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003760 break;
3761 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3762 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003763 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003764 msrs->entry_ctls_low,
3765 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003766 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3767 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003768 break;
3769 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003770 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003771 msrs->misc_low,
3772 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003773 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003774 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003775 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003776 break;
3777 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003778 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003779 break;
3780 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003781 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003782 break;
3783 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003784 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003785 break;
3786 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003787 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003788 break;
3789 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003790 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003791 msrs->secondary_ctls_low,
3792 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003793 break;
3794 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003795 *pdata = msrs->ept_caps |
3796 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003797 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003798 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003799 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003800 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003801 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003802 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003803 }
3804
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003805 return 0;
3806}
3807
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003808static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3809 uint64_t val)
3810{
3811 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3812
3813 return !(val & ~valid_bits);
3814}
3815
Tom Lendacky801e4592018-02-21 13:39:51 -06003816static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3817{
Paolo Bonzini13893092018-02-26 13:40:09 +01003818 switch (msr->index) {
3819 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3820 if (!nested)
3821 return 1;
3822 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3823 default:
3824 return 1;
3825 }
3826
3827 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003828}
3829
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003830/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831 * Reads an msr value (of 'msr_index') into 'pdata'.
3832 * Returns 0 on success, non-0 otherwise.
3833 * Assumes vcpu_load() was already called.
3834 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003835static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003837 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003838 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003839
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003840 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003841#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003842 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003843 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844 break;
3845 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003846 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003848 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003849 vmx_load_host_state(vmx);
3850 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003851 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003852#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003853 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003854 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003855 case MSR_IA32_SPEC_CTRL:
3856 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003857 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3858 return 1;
3859
3860 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3861 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003862 case MSR_IA32_ARCH_CAPABILITIES:
3863 if (!msr_info->host_initiated &&
3864 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3865 return 1;
3866 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3867 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003868 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003869 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003870 break;
3871 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003872 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003873 break;
3874 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003875 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003877 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003878 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003879 (!msr_info->host_initiated &&
3880 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003881 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003882 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003883 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003884 case MSR_IA32_MCG_EXT_CTL:
3885 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003886 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003887 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003888 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003889 msr_info->data = vcpu->arch.mcg_ext_ctl;
3890 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003891 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003892 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003893 break;
3894 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3895 if (!nested_vmx_allowed(vcpu))
3896 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003897 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3898 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003899 case MSR_IA32_XSS:
3900 if (!vmx_xsaves_supported())
3901 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003902 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003903 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003904 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003905 if (!msr_info->host_initiated &&
3906 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003907 return 1;
3908 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003909 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003910 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003911 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003912 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003913 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003914 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003915 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003916 }
3917
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918 return 0;
3919}
3920
Jan Kiszkacae50132014-01-04 18:47:22 +01003921static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3922
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923/*
3924 * Writes msr value into into the appropriate "register".
3925 * Returns 0 on success, non-0 otherwise.
3926 * Assumes vcpu_load() was already called.
3927 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003928static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003929{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003930 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003931 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003932 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003933 u32 msr_index = msr_info->index;
3934 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003935
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003937 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003938 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003939 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003940#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003942 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003943 vmcs_writel(GUEST_FS_BASE, data);
3944 break;
3945 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003946 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003947 vmcs_writel(GUEST_GS_BASE, data);
3948 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003949 case MSR_KERNEL_GS_BASE:
3950 vmx_load_host_state(vmx);
3951 vmx->msr_guest_kernel_gs_base = data;
3952 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003953#endif
3954 case MSR_IA32_SYSENTER_CS:
3955 vmcs_write32(GUEST_SYSENTER_CS, data);
3956 break;
3957 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003958 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003959 break;
3960 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003961 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003963 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003964 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003965 (!msr_info->host_initiated &&
3966 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003967 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003968 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003969 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003970 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003971 vmcs_write64(GUEST_BNDCFGS, data);
3972 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003973 case MSR_IA32_SPEC_CTRL:
3974 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003975 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3976 return 1;
3977
3978 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003979 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003980 return 1;
3981
3982 vmx->spec_ctrl = data;
3983
3984 if (!data)
3985 break;
3986
3987 /*
3988 * For non-nested:
3989 * When it's written (to non-zero) for the first time, pass
3990 * it through.
3991 *
3992 * For nested:
3993 * The handling of the MSR bitmap for L2 guests is done in
3994 * nested_vmx_merge_msr_bitmap. We should not touch the
3995 * vmcs02.msr_bitmap here since it gets completely overwritten
3996 * in the merging. We update the vmcs01 here for L1 as well
3997 * since it will end up touching the MSR anyway now.
3998 */
3999 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4000 MSR_IA32_SPEC_CTRL,
4001 MSR_TYPE_RW);
4002 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004003 case MSR_IA32_PRED_CMD:
4004 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004005 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4006 return 1;
4007
4008 if (data & ~PRED_CMD_IBPB)
4009 return 1;
4010
4011 if (!data)
4012 break;
4013
4014 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4015
4016 /*
4017 * For non-nested:
4018 * When it's written (to non-zero) for the first time, pass
4019 * it through.
4020 *
4021 * For nested:
4022 * The handling of the MSR bitmap for L2 guests is done in
4023 * nested_vmx_merge_msr_bitmap. We should not touch the
4024 * vmcs02.msr_bitmap here since it gets completely overwritten
4025 * in the merging.
4026 */
4027 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4028 MSR_TYPE_W);
4029 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004030 case MSR_IA32_ARCH_CAPABILITIES:
4031 if (!msr_info->host_initiated)
4032 return 1;
4033 vmx->arch_capabilities = data;
4034 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004035 case MSR_IA32_CR_PAT:
4036 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004037 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4038 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004039 vmcs_write64(GUEST_IA32_PAT, data);
4040 vcpu->arch.pat = data;
4041 break;
4042 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004043 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004044 break;
Will Auldba904632012-11-29 12:42:50 -08004045 case MSR_IA32_TSC_ADJUST:
4046 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004047 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004048 case MSR_IA32_MCG_EXT_CTL:
4049 if ((!msr_info->host_initiated &&
4050 !(to_vmx(vcpu)->msr_ia32_feature_control &
4051 FEATURE_CONTROL_LMCE)) ||
4052 (data & ~MCG_EXT_CTL_LMCE_EN))
4053 return 1;
4054 vcpu->arch.mcg_ext_ctl = data;
4055 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004056 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004057 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004058 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004059 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4060 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004061 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004062 if (msr_info->host_initiated && data == 0)
4063 vmx_leave_nested(vcpu);
4064 break;
4065 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004066 if (!msr_info->host_initiated)
4067 return 1; /* they are read-only */
4068 if (!nested_vmx_allowed(vcpu))
4069 return 1;
4070 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004071 case MSR_IA32_XSS:
4072 if (!vmx_xsaves_supported())
4073 return 1;
4074 /*
4075 * The only supported bit as of Skylake is bit 8, but
4076 * it is not supported on KVM.
4077 */
4078 if (data != 0)
4079 return 1;
4080 vcpu->arch.ia32_xss = data;
4081 if (vcpu->arch.ia32_xss != host_xss)
4082 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4083 vcpu->arch.ia32_xss, host_xss);
4084 else
4085 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4086 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004087 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004088 if (!msr_info->host_initiated &&
4089 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004090 return 1;
4091 /* Check reserved bit, higher 32 bits should be zero */
4092 if ((data >> 32) != 0)
4093 return 1;
4094 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004096 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004097 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004098 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004099 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004100 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4101 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004102 ret = kvm_set_shared_msr(msr->index, msr->data,
4103 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004104 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004105 if (ret)
4106 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004107 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004108 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004109 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004110 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111 }
4112
Eddie Dong2cc51562007-05-21 07:28:09 +03004113 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004114}
4115
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004116static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004117{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004118 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4119 switch (reg) {
4120 case VCPU_REGS_RSP:
4121 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4122 break;
4123 case VCPU_REGS_RIP:
4124 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4125 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004126 case VCPU_EXREG_PDPTR:
4127 if (enable_ept)
4128 ept_save_pdptrs(vcpu);
4129 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004130 default:
4131 break;
4132 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133}
4134
Avi Kivity6aa8b732006-12-10 02:21:36 -08004135static __init int cpu_has_kvm_support(void)
4136{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004137 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138}
4139
4140static __init int vmx_disabled_by_bios(void)
4141{
4142 u64 msr;
4143
4144 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004145 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004146 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004147 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4148 && tboot_enabled())
4149 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004150 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004151 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004152 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004153 && !tboot_enabled()) {
4154 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004155 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004156 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004157 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004158 /* launched w/o TXT and VMX disabled */
4159 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4160 && !tboot_enabled())
4161 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004162 }
4163
4164 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165}
4166
Dongxiao Xu7725b892010-05-11 18:29:38 +08004167static void kvm_cpu_vmxon(u64 addr)
4168{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004169 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004170 intel_pt_handle_vmx(1);
4171
Dongxiao Xu7725b892010-05-11 18:29:38 +08004172 asm volatile (ASM_VMX_VMXON_RAX
4173 : : "a"(&addr), "m"(addr)
4174 : "memory", "cc");
4175}
4176
Radim Krčmář13a34e02014-08-28 15:13:03 +02004177static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178{
4179 int cpu = raw_smp_processor_id();
4180 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004181 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004183 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004184 return -EBUSY;
4185
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004186 /*
4187 * This can happen if we hot-added a CPU but failed to allocate
4188 * VP assist page for it.
4189 */
4190 if (static_branch_unlikely(&enable_evmcs) &&
4191 !hv_get_vp_assist_page(cpu))
4192 return -EFAULT;
4193
Nadav Har'Eld462b812011-05-24 15:26:10 +03004194 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004195 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4196 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004197
4198 /*
4199 * Now we can enable the vmclear operation in kdump
4200 * since the loaded_vmcss_on_cpu list on this cpu
4201 * has been initialized.
4202 *
4203 * Though the cpu is not in VMX operation now, there
4204 * is no problem to enable the vmclear operation
4205 * for the loaded_vmcss_on_cpu list is empty!
4206 */
4207 crash_enable_local_vmclear(cpu);
4208
Avi Kivity6aa8b732006-12-10 02:21:36 -08004209 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004210
4211 test_bits = FEATURE_CONTROL_LOCKED;
4212 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4213 if (tboot_enabled())
4214 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4215
4216 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004217 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004218 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4219 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004220 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004221 if (enable_ept)
4222 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004223
4224 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004225}
4226
Nadav Har'Eld462b812011-05-24 15:26:10 +03004227static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004228{
4229 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004230 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004231
Nadav Har'Eld462b812011-05-24 15:26:10 +03004232 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4233 loaded_vmcss_on_cpu_link)
4234 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004235}
4236
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004237
4238/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4239 * tricks.
4240 */
4241static void kvm_cpu_vmxoff(void)
4242{
4243 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004244
4245 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004246 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004247}
4248
Radim Krčmář13a34e02014-08-28 15:13:03 +02004249static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004250{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004251 vmclear_local_loaded_vmcss();
4252 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253}
4254
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004255static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004256 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004257{
4258 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004259 u32 ctl = ctl_min | ctl_opt;
4260
4261 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4262
4263 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4264 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4265
4266 /* Ensure minimum (required) set of control bits are supported. */
4267 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004268 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004269
4270 *result = ctl;
4271 return 0;
4272}
4273
Avi Kivity110312c2010-12-21 12:54:20 +02004274static __init bool allow_1_setting(u32 msr, u32 ctl)
4275{
4276 u32 vmx_msr_low, vmx_msr_high;
4277
4278 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4279 return vmx_msr_high & ctl;
4280}
4281
Yang, Sheng002c7f72007-07-31 14:23:01 +03004282static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004283{
4284 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004285 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004286 u32 _pin_based_exec_control = 0;
4287 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004288 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004289 u32 _vmexit_control = 0;
4290 u32 _vmentry_control = 0;
4291
Paolo Bonzini13893092018-02-26 13:40:09 +01004292 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304293 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004294#ifdef CONFIG_X86_64
4295 CPU_BASED_CR8_LOAD_EXITING |
4296 CPU_BASED_CR8_STORE_EXITING |
4297#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004298 CPU_BASED_CR3_LOAD_EXITING |
4299 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004300 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004301 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004302 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004303 CPU_BASED_MWAIT_EXITING |
4304 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004305 CPU_BASED_INVLPG_EXITING |
4306 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004307
Sheng Yangf78e0e22007-10-29 09:40:42 +08004308 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004309 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004310 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004311 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4312 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004313 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004314#ifdef CONFIG_X86_64
4315 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4316 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4317 ~CPU_BASED_CR8_STORE_EXITING;
4318#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004319 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004320 min2 = 0;
4321 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004322 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004323 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004324 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004325 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004326 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004327 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004328 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004329 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004330 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004331 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004332 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004333 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004334 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004335 SECONDARY_EXEC_RDSEED_EXITING |
4336 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004337 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004338 SECONDARY_EXEC_TSC_SCALING |
4339 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004340 if (adjust_vmx_controls(min2, opt2,
4341 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004342 &_cpu_based_2nd_exec_control) < 0)
4343 return -EIO;
4344 }
4345#ifndef CONFIG_X86_64
4346 if (!(_cpu_based_2nd_exec_control &
4347 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4348 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4349#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004350
4351 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4352 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004353 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004354 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4355 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004356
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004357 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4358 &vmx_capability.ept, &vmx_capability.vpid);
4359
Sheng Yangd56f5462008-04-25 10:13:16 +08004360 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004361 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4362 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004363 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4364 CPU_BASED_CR3_STORE_EXITING |
4365 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004366 } else if (vmx_capability.ept) {
4367 vmx_capability.ept = 0;
4368 pr_warn_once("EPT CAP should not exist if not support "
4369 "1-setting enable EPT VM-execution control\n");
4370 }
4371 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4372 vmx_capability.vpid) {
4373 vmx_capability.vpid = 0;
4374 pr_warn_once("VPID CAP should not exist if not support "
4375 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004376 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004377
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004378 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004379#ifdef CONFIG_X86_64
4380 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4381#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004382 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004383 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004384 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4385 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004386 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004387
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004388 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4389 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4390 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004391 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4392 &_pin_based_exec_control) < 0)
4393 return -EIO;
4394
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004395 if (cpu_has_broken_vmx_preemption_timer())
4396 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004397 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004398 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004399 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4400
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004401 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004402 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004403 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4404 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004405 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004406
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004407 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004408
4409 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4410 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004411 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004412
4413#ifdef CONFIG_X86_64
4414 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4415 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004416 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004417#endif
4418
4419 /* Require Write-Back (WB) memory type for VMCS accesses. */
4420 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004421 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004422
Yang, Sheng002c7f72007-07-31 14:23:01 +03004423 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004424 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004425 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004426
Liran Alon2307af12018-06-29 22:59:04 +03004427 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004428
Yang, Sheng002c7f72007-07-31 14:23:01 +03004429 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4430 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004431 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004432 vmcs_conf->vmexit_ctrl = _vmexit_control;
4433 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004434
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004435 if (static_branch_unlikely(&enable_evmcs))
4436 evmcs_sanitize_exec_ctrls(vmcs_conf);
4437
Avi Kivity110312c2010-12-21 12:54:20 +02004438 cpu_has_load_ia32_efer =
4439 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4440 VM_ENTRY_LOAD_IA32_EFER)
4441 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4442 VM_EXIT_LOAD_IA32_EFER);
4443
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004444 cpu_has_load_perf_global_ctrl =
4445 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4446 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4447 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4448 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4449
4450 /*
4451 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004452 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004453 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4454 *
4455 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4456 *
4457 * AAK155 (model 26)
4458 * AAP115 (model 30)
4459 * AAT100 (model 37)
4460 * BC86,AAY89,BD102 (model 44)
4461 * BA97 (model 46)
4462 *
4463 */
4464 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4465 switch (boot_cpu_data.x86_model) {
4466 case 26:
4467 case 30:
4468 case 37:
4469 case 44:
4470 case 46:
4471 cpu_has_load_perf_global_ctrl = false;
4472 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4473 "does not work properly. Using workaround\n");
4474 break;
4475 default:
4476 break;
4477 }
4478 }
4479
Borislav Petkov782511b2016-04-04 22:25:03 +02004480 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004481 rdmsrl(MSR_IA32_XSS, host_xss);
4482
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004483 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004484}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004485
Liran Alon491a6032018-06-23 02:35:12 +03004486static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004487{
4488 int node = cpu_to_node(cpu);
4489 struct page *pages;
4490 struct vmcs *vmcs;
4491
Vlastimil Babka96db8002015-09-08 15:03:50 -07004492 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493 if (!pages)
4494 return NULL;
4495 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004496 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004497
4498 /* KVM supports Enlightened VMCS v1 only */
4499 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004500 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004501 else
Liran Alon392b2f22018-06-23 02:35:01 +03004502 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004503
Liran Alon491a6032018-06-23 02:35:12 +03004504 if (shadow)
4505 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004506 return vmcs;
4507}
4508
Avi Kivity6aa8b732006-12-10 02:21:36 -08004509static void free_vmcs(struct vmcs *vmcs)
4510{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004511 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512}
4513
Nadav Har'Eld462b812011-05-24 15:26:10 +03004514/*
4515 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4516 */
4517static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4518{
4519 if (!loaded_vmcs->vmcs)
4520 return;
4521 loaded_vmcs_clear(loaded_vmcs);
4522 free_vmcs(loaded_vmcs->vmcs);
4523 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004524 if (loaded_vmcs->msr_bitmap)
4525 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004526 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004527}
4528
Liran Alon491a6032018-06-23 02:35:12 +03004529static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004530{
Liran Alon491a6032018-06-23 02:35:12 +03004531 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004532}
4533
4534static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4535{
Liran Alon491a6032018-06-23 02:35:12 +03004536 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004537 if (!loaded_vmcs->vmcs)
4538 return -ENOMEM;
4539
4540 loaded_vmcs->shadow_vmcs = NULL;
4541 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004542
4543 if (cpu_has_vmx_msr_bitmap()) {
4544 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4545 if (!loaded_vmcs->msr_bitmap)
4546 goto out_vmcs;
4547 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004548
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004549 if (IS_ENABLED(CONFIG_HYPERV) &&
4550 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004551 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4552 struct hv_enlightened_vmcs *evmcs =
4553 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4554
4555 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4556 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004557 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004558 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004559
4560out_vmcs:
4561 free_loaded_vmcs(loaded_vmcs);
4562 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004563}
4564
Sam Ravnborg39959582007-06-01 00:47:13 -07004565static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004566{
4567 int cpu;
4568
Zachary Amsden3230bb42009-09-29 11:38:37 -10004569 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004570 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004571 per_cpu(vmxarea, cpu) = NULL;
4572 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004573}
4574
Jim Mattsond37f4262017-12-22 12:12:16 -08004575enum vmcs_field_width {
4576 VMCS_FIELD_WIDTH_U16 = 0,
4577 VMCS_FIELD_WIDTH_U64 = 1,
4578 VMCS_FIELD_WIDTH_U32 = 2,
4579 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004580};
4581
Jim Mattsond37f4262017-12-22 12:12:16 -08004582static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004583{
4584 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004585 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004586 return (field >> 13) & 0x3 ;
4587}
4588
4589static inline int vmcs_field_readonly(unsigned long field)
4590{
4591 return (((field >> 10) & 0x3) == 1);
4592}
4593
Bandan Dasfe2b2012014-04-21 15:20:14 -04004594static void init_vmcs_shadow_fields(void)
4595{
4596 int i, j;
4597
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004598 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4599 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004600 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004601 (i + 1 == max_shadow_read_only_fields ||
4602 shadow_read_only_fields[i + 1] != field + 1))
4603 pr_err("Missing field from shadow_read_only_field %x\n",
4604 field + 1);
4605
4606 clear_bit(field, vmx_vmread_bitmap);
4607#ifdef CONFIG_X86_64
4608 if (field & 1)
4609 continue;
4610#endif
4611 if (j < i)
4612 shadow_read_only_fields[j] = field;
4613 j++;
4614 }
4615 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004616
4617 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004618 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004619 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004620 (i + 1 == max_shadow_read_write_fields ||
4621 shadow_read_write_fields[i + 1] != field + 1))
4622 pr_err("Missing field from shadow_read_write_field %x\n",
4623 field + 1);
4624
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004625 /*
4626 * PML and the preemption timer can be emulated, but the
4627 * processor cannot vmwrite to fields that don't exist
4628 * on bare metal.
4629 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004630 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004631 case GUEST_PML_INDEX:
4632 if (!cpu_has_vmx_pml())
4633 continue;
4634 break;
4635 case VMX_PREEMPTION_TIMER_VALUE:
4636 if (!cpu_has_vmx_preemption_timer())
4637 continue;
4638 break;
4639 case GUEST_INTR_STATUS:
4640 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004641 continue;
4642 break;
4643 default:
4644 break;
4645 }
4646
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004647 clear_bit(field, vmx_vmwrite_bitmap);
4648 clear_bit(field, vmx_vmread_bitmap);
4649#ifdef CONFIG_X86_64
4650 if (field & 1)
4651 continue;
4652#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004653 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004654 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004655 j++;
4656 }
4657 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004658}
4659
Avi Kivity6aa8b732006-12-10 02:21:36 -08004660static __init int alloc_kvm_area(void)
4661{
4662 int cpu;
4663
Zachary Amsden3230bb42009-09-29 11:38:37 -10004664 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004665 struct vmcs *vmcs;
4666
Liran Alon491a6032018-06-23 02:35:12 +03004667 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004668 if (!vmcs) {
4669 free_kvm_area();
4670 return -ENOMEM;
4671 }
4672
Liran Alon2307af12018-06-29 22:59:04 +03004673 /*
4674 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4675 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4676 * revision_id reported by MSR_IA32_VMX_BASIC.
4677 *
4678 * However, even though not explictly documented by
4679 * TLFS, VMXArea passed as VMXON argument should
4680 * still be marked with revision_id reported by
4681 * physical CPU.
4682 */
4683 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004684 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004685
Avi Kivity6aa8b732006-12-10 02:21:36 -08004686 per_cpu(vmxarea, cpu) = vmcs;
4687 }
4688 return 0;
4689}
4690
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004691static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004692 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004693{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004694 if (!emulate_invalid_guest_state) {
4695 /*
4696 * CS and SS RPL should be equal during guest entry according
4697 * to VMX spec, but in reality it is not always so. Since vcpu
4698 * is in the middle of the transition from real mode to
4699 * protected mode it is safe to assume that RPL 0 is a good
4700 * default value.
4701 */
4702 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004703 save->selector &= ~SEGMENT_RPL_MASK;
4704 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004705 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004706 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004707 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708}
4709
4710static void enter_pmode(struct kvm_vcpu *vcpu)
4711{
4712 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004713 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004714
Gleb Natapovd99e4152012-12-20 16:57:45 +02004715 /*
4716 * Update real mode segment cache. It may be not up-to-date if sement
4717 * register was written while vcpu was in a guest mode.
4718 */
4719 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4720 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4721 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4722 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4723 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4724 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4725
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004726 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004727
Avi Kivity2fb92db2011-04-27 19:42:18 +03004728 vmx_segment_cache_clear(vmx);
4729
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004730 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004731
4732 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004733 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4734 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004735 vmcs_writel(GUEST_RFLAGS, flags);
4736
Rusty Russell66aee912007-07-17 23:34:16 +10004737 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4738 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004739
4740 update_exception_bitmap(vcpu);
4741
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004742 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4743 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4744 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4745 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4746 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4747 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004748}
4749
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004750static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004751{
Mathias Krause772e0312012-08-30 01:30:19 +02004752 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004753 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004754
Gleb Natapovd99e4152012-12-20 16:57:45 +02004755 var.dpl = 0x3;
4756 if (seg == VCPU_SREG_CS)
4757 var.type = 0x3;
4758
4759 if (!emulate_invalid_guest_state) {
4760 var.selector = var.base >> 4;
4761 var.base = var.base & 0xffff0;
4762 var.limit = 0xffff;
4763 var.g = 0;
4764 var.db = 0;
4765 var.present = 1;
4766 var.s = 1;
4767 var.l = 0;
4768 var.unusable = 0;
4769 var.type = 0x3;
4770 var.avl = 0;
4771 if (save->base & 0xf)
4772 printk_once(KERN_WARNING "kvm: segment base is not "
4773 "paragraph aligned when entering "
4774 "protected mode (seg=%d)", seg);
4775 }
4776
4777 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004778 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004779 vmcs_write32(sf->limit, var.limit);
4780 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004781}
4782
4783static void enter_rmode(struct kvm_vcpu *vcpu)
4784{
4785 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004786 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004787 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004789 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4790 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4791 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4792 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4793 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004794 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4795 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004796
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004797 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004798
Gleb Natapov776e58e2011-03-13 12:34:27 +02004799 /*
4800 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004801 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004802 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004803 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004804 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4805 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004806
Avi Kivity2fb92db2011-04-27 19:42:18 +03004807 vmx_segment_cache_clear(vmx);
4808
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004809 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004810 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004811 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4812
4813 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004814 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004815
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004816 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004817
4818 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004819 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820 update_exception_bitmap(vcpu);
4821
Gleb Natapovd99e4152012-12-20 16:57:45 +02004822 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4823 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4824 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4825 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4826 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4827 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004828
Eddie Dong8668a3c2007-10-10 14:26:45 +08004829 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004830}
4831
Amit Shah401d10d2009-02-20 22:53:37 +05304832static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4833{
4834 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004835 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4836
4837 if (!msr)
4838 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304839
Avi Kivity44ea2b12009-09-06 15:55:37 +03004840 /*
4841 * Force kernel_gs_base reloading before EFER changes, as control
4842 * of this msr depends on is_long_mode().
4843 */
4844 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004845 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304846 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004847 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304848 msr->data = efer;
4849 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004850 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304851
4852 msr->data = efer & ~EFER_LME;
4853 }
4854 setup_msrs(vmx);
4855}
4856
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004857#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004858
4859static void enter_lmode(struct kvm_vcpu *vcpu)
4860{
4861 u32 guest_tr_ar;
4862
Avi Kivity2fb92db2011-04-27 19:42:18 +03004863 vmx_segment_cache_clear(to_vmx(vcpu));
4864
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004866 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004867 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4868 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004870 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4871 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872 }
Avi Kivityda38f432010-07-06 11:30:49 +03004873 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874}
4875
4876static void exit_lmode(struct kvm_vcpu *vcpu)
4877{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004878 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004879 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004880}
4881
4882#endif
4883
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004884static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4885 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004886{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004887 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004888 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4889 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004890 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004891 } else {
4892 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004893 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004894}
4895
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004896static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004897{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004898 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004899}
4900
Junaid Shahidfaff8752018-06-29 13:10:05 -07004901static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
4902{
4903 int vpid = to_vmx(vcpu)->vpid;
4904
4905 if (!vpid_sync_vcpu_addr(vpid, addr))
4906 vpid_sync_context(vpid);
4907
4908 /*
4909 * If VPIDs are not supported or enabled, then the above is a no-op.
4910 * But we don't really need a TLB flush in that case anyway, because
4911 * each VM entry/exit includes an implicit flush when VPID is 0.
4912 */
4913}
4914
Avi Kivitye8467fd2009-12-29 18:43:06 +02004915static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4916{
4917 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4918
4919 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4920 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4921}
4922
Avi Kivityaff48ba2010-12-05 18:56:11 +02004923static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4924{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004925 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004926 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4927 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4928}
4929
Anthony Liguori25c4c272007-04-27 09:29:21 +03004930static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004931{
Avi Kivityfc78f512009-12-07 12:16:48 +02004932 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4933
4934 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4935 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004936}
4937
Sheng Yang14394422008-04-28 12:24:45 +08004938static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4939{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004940 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4941
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004942 if (!test_bit(VCPU_EXREG_PDPTR,
4943 (unsigned long *)&vcpu->arch.regs_dirty))
4944 return;
4945
Sheng Yang14394422008-04-28 12:24:45 +08004946 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004947 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4948 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4949 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4950 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004951 }
4952}
4953
Avi Kivity8f5d5492009-05-31 18:41:29 +03004954static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4955{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004956 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4957
Avi Kivity8f5d5492009-05-31 18:41:29 +03004958 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004959 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4960 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4961 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4962 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004963 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004964
4965 __set_bit(VCPU_EXREG_PDPTR,
4966 (unsigned long *)&vcpu->arch.regs_avail);
4967 __set_bit(VCPU_EXREG_PDPTR,
4968 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004969}
4970
David Matlack38991522016-11-29 18:14:08 -08004971static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4972{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004973 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4974 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004975 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4976
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004977 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004978 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4979 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4980 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4981
4982 return fixed_bits_valid(val, fixed0, fixed1);
4983}
4984
4985static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4986{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004987 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4988 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004989
4990 return fixed_bits_valid(val, fixed0, fixed1);
4991}
4992
4993static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4994{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004995 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4996 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004997
4998 return fixed_bits_valid(val, fixed0, fixed1);
4999}
5000
5001/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5002#define nested_guest_cr4_valid nested_cr4_valid
5003#define nested_host_cr4_valid nested_cr4_valid
5004
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005005static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005006
5007static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5008 unsigned long cr0,
5009 struct kvm_vcpu *vcpu)
5010{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005011 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5012 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005013 if (!(cr0 & X86_CR0_PG)) {
5014 /* From paging/starting to nonpaging */
5015 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005016 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005017 (CPU_BASED_CR3_LOAD_EXITING |
5018 CPU_BASED_CR3_STORE_EXITING));
5019 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005020 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005021 } else if (!is_paging(vcpu)) {
5022 /* From nonpaging to paging */
5023 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005024 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005025 ~(CPU_BASED_CR3_LOAD_EXITING |
5026 CPU_BASED_CR3_STORE_EXITING));
5027 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005028 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005029 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005030
5031 if (!(cr0 & X86_CR0_WP))
5032 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005033}
5034
Avi Kivity6aa8b732006-12-10 02:21:36 -08005035static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5036{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005037 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005038 unsigned long hw_cr0;
5039
Gleb Natapov50378782013-02-04 16:00:28 +02005040 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005041 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005042 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005043 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005044 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005045
Gleb Natapov218e7632013-01-21 15:36:45 +02005046 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5047 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048
Gleb Natapov218e7632013-01-21 15:36:45 +02005049 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5050 enter_rmode(vcpu);
5051 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005052
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005053#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005054 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005055 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005056 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005057 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005058 exit_lmode(vcpu);
5059 }
5060#endif
5061
Sean Christophersonb4d18512018-03-05 12:04:40 -08005062 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005063 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5064
Avi Kivity6aa8b732006-12-10 02:21:36 -08005065 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005066 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005067 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005068
5069 /* depends on vcpu->arch.cr0 to be set to a new value */
5070 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005071}
5072
Yu Zhang855feb62017-08-24 20:27:55 +08005073static int get_ept_level(struct kvm_vcpu *vcpu)
5074{
5075 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5076 return 5;
5077 return 4;
5078}
5079
Peter Feiner995f00a2017-06-30 17:26:32 -07005080static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005081{
Yu Zhang855feb62017-08-24 20:27:55 +08005082 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005083
Yu Zhang855feb62017-08-24 20:27:55 +08005084 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005085
Peter Feiner995f00a2017-06-30 17:26:32 -07005086 if (enable_ept_ad_bits &&
5087 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005088 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005089 eptp |= (root_hpa & PAGE_MASK);
5090
5091 return eptp;
5092}
5093
Avi Kivity6aa8b732006-12-10 02:21:36 -08005094static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5095{
Tianyu Lan877ad952018-07-19 08:40:23 +00005096 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005097 unsigned long guest_cr3;
5098 u64 eptp;
5099
5100 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005101 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005102 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005103 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005104
5105 if (kvm_x86_ops->tlb_remote_flush) {
5106 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5107 to_vmx(vcpu)->ept_pointer = eptp;
5108 to_kvm_vmx(kvm)->ept_pointers_match
5109 = EPT_POINTERS_CHECK;
5110 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5111 }
5112
Sean Christophersone90008d2018-03-05 12:04:37 -08005113 if (enable_unrestricted_guest || is_paging(vcpu) ||
5114 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005115 guest_cr3 = kvm_read_cr3(vcpu);
5116 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005117 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005118 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005119 }
5120
Sheng Yang14394422008-04-28 12:24:45 +08005121 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122}
5123
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005124static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005125{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005126 /*
5127 * Pass through host's Machine Check Enable value to hw_cr4, which
5128 * is in force while we are in guest mode. Do not let guests control
5129 * this bit, even if host CR4.MCE == 0.
5130 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005131 unsigned long hw_cr4;
5132
5133 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5134 if (enable_unrestricted_guest)
5135 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5136 else if (to_vmx(vcpu)->rmode.vm86_active)
5137 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5138 else
5139 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005140
Sean Christopherson64f7a112018-04-30 10:01:06 -07005141 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5142 if (cr4 & X86_CR4_UMIP) {
5143 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005144 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005145 hw_cr4 &= ~X86_CR4_UMIP;
5146 } else if (!is_guest_mode(vcpu) ||
5147 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5148 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5149 SECONDARY_EXEC_DESC);
5150 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005151
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005152 if (cr4 & X86_CR4_VMXE) {
5153 /*
5154 * To use VMXON (and later other VMX instructions), a guest
5155 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5156 * So basically the check on whether to allow nested VMX
5157 * is here.
5158 */
5159 if (!nested_vmx_allowed(vcpu))
5160 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005161 }
David Matlack38991522016-11-29 18:14:08 -08005162
5163 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005164 return 1;
5165
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005166 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005167
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005168 if (!enable_unrestricted_guest) {
5169 if (enable_ept) {
5170 if (!is_paging(vcpu)) {
5171 hw_cr4 &= ~X86_CR4_PAE;
5172 hw_cr4 |= X86_CR4_PSE;
5173 } else if (!(cr4 & X86_CR4_PAE)) {
5174 hw_cr4 &= ~X86_CR4_PAE;
5175 }
5176 }
5177
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005178 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005179 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5180 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5181 * to be manually disabled when guest switches to non-paging
5182 * mode.
5183 *
5184 * If !enable_unrestricted_guest, the CPU is always running
5185 * with CR0.PG=1 and CR4 needs to be modified.
5186 * If enable_unrestricted_guest, the CPU automatically
5187 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005188 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005189 if (!is_paging(vcpu))
5190 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5191 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005192
Sheng Yang14394422008-04-28 12:24:45 +08005193 vmcs_writel(CR4_READ_SHADOW, cr4);
5194 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005195 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005196}
5197
Avi Kivity6aa8b732006-12-10 02:21:36 -08005198static void vmx_get_segment(struct kvm_vcpu *vcpu,
5199 struct kvm_segment *var, int seg)
5200{
Avi Kivitya9179492011-01-03 14:28:52 +02005201 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005202 u32 ar;
5203
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005204 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005205 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005206 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005207 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005208 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005209 var->base = vmx_read_guest_seg_base(vmx, seg);
5210 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5211 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005212 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005213 var->base = vmx_read_guest_seg_base(vmx, seg);
5214 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5215 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5216 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005217 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005218 var->type = ar & 15;
5219 var->s = (ar >> 4) & 1;
5220 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005221 /*
5222 * Some userspaces do not preserve unusable property. Since usable
5223 * segment has to be present according to VMX spec we can use present
5224 * property to amend userspace bug by making unusable segment always
5225 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5226 * segment as unusable.
5227 */
5228 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005229 var->avl = (ar >> 12) & 1;
5230 var->l = (ar >> 13) & 1;
5231 var->db = (ar >> 14) & 1;
5232 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005233}
5234
Avi Kivitya9179492011-01-03 14:28:52 +02005235static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5236{
Avi Kivitya9179492011-01-03 14:28:52 +02005237 struct kvm_segment s;
5238
5239 if (to_vmx(vcpu)->rmode.vm86_active) {
5240 vmx_get_segment(vcpu, &s, seg);
5241 return s.base;
5242 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005243 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005244}
5245
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005246static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005247{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005248 struct vcpu_vmx *vmx = to_vmx(vcpu);
5249
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005250 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005251 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005252 else {
5253 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005254 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005255 }
Avi Kivity69c73022011-03-07 15:26:44 +02005256}
5257
Avi Kivity653e3102007-05-07 10:55:37 +03005258static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005259{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005260 u32 ar;
5261
Avi Kivityf0495f92012-06-07 17:06:10 +03005262 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005263 ar = 1 << 16;
5264 else {
5265 ar = var->type & 15;
5266 ar |= (var->s & 1) << 4;
5267 ar |= (var->dpl & 3) << 5;
5268 ar |= (var->present & 1) << 7;
5269 ar |= (var->avl & 1) << 12;
5270 ar |= (var->l & 1) << 13;
5271 ar |= (var->db & 1) << 14;
5272 ar |= (var->g & 1) << 15;
5273 }
Avi Kivity653e3102007-05-07 10:55:37 +03005274
5275 return ar;
5276}
5277
5278static void vmx_set_segment(struct kvm_vcpu *vcpu,
5279 struct kvm_segment *var, int seg)
5280{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005281 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005282 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005283
Avi Kivity2fb92db2011-04-27 19:42:18 +03005284 vmx_segment_cache_clear(vmx);
5285
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005286 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5287 vmx->rmode.segs[seg] = *var;
5288 if (seg == VCPU_SREG_TR)
5289 vmcs_write16(sf->selector, var->selector);
5290 else if (var->s)
5291 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005292 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005293 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005294
Avi Kivity653e3102007-05-07 10:55:37 +03005295 vmcs_writel(sf->base, var->base);
5296 vmcs_write32(sf->limit, var->limit);
5297 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005298
5299 /*
5300 * Fix the "Accessed" bit in AR field of segment registers for older
5301 * qemu binaries.
5302 * IA32 arch specifies that at the time of processor reset the
5303 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005304 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005305 * state vmexit when "unrestricted guest" mode is turned on.
5306 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5307 * tree. Newer qemu binaries with that qemu fix would not need this
5308 * kvm hack.
5309 */
5310 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005311 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005312
Gleb Natapovf924d662012-12-12 19:10:55 +02005313 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005314
5315out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005316 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005317}
5318
Avi Kivity6aa8b732006-12-10 02:21:36 -08005319static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5320{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005321 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005322
5323 *db = (ar >> 14) & 1;
5324 *l = (ar >> 13) & 1;
5325}
5326
Gleb Natapov89a27f42010-02-16 10:51:48 +02005327static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005328{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005329 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5330 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005331}
5332
Gleb Natapov89a27f42010-02-16 10:51:48 +02005333static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005334{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005335 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5336 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005337}
5338
Gleb Natapov89a27f42010-02-16 10:51:48 +02005339static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005340{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005341 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5342 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005343}
5344
Gleb Natapov89a27f42010-02-16 10:51:48 +02005345static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005346{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005347 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5348 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005349}
5350
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005351static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5352{
5353 struct kvm_segment var;
5354 u32 ar;
5355
5356 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005357 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005358 if (seg == VCPU_SREG_CS)
5359 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005360 ar = vmx_segment_access_rights(&var);
5361
5362 if (var.base != (var.selector << 4))
5363 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005364 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005365 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005366 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005367 return false;
5368
5369 return true;
5370}
5371
5372static bool code_segment_valid(struct kvm_vcpu *vcpu)
5373{
5374 struct kvm_segment cs;
5375 unsigned int cs_rpl;
5376
5377 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005378 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005379
Avi Kivity1872a3f2009-01-04 23:26:52 +02005380 if (cs.unusable)
5381 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005382 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005383 return false;
5384 if (!cs.s)
5385 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005386 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005387 if (cs.dpl > cs_rpl)
5388 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005389 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005390 if (cs.dpl != cs_rpl)
5391 return false;
5392 }
5393 if (!cs.present)
5394 return false;
5395
5396 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5397 return true;
5398}
5399
5400static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5401{
5402 struct kvm_segment ss;
5403 unsigned int ss_rpl;
5404
5405 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005406 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005407
Avi Kivity1872a3f2009-01-04 23:26:52 +02005408 if (ss.unusable)
5409 return true;
5410 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005411 return false;
5412 if (!ss.s)
5413 return false;
5414 if (ss.dpl != ss_rpl) /* DPL != RPL */
5415 return false;
5416 if (!ss.present)
5417 return false;
5418
5419 return true;
5420}
5421
5422static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5423{
5424 struct kvm_segment var;
5425 unsigned int rpl;
5426
5427 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005428 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005429
Avi Kivity1872a3f2009-01-04 23:26:52 +02005430 if (var.unusable)
5431 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005432 if (!var.s)
5433 return false;
5434 if (!var.present)
5435 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005436 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005437 if (var.dpl < rpl) /* DPL < RPL */
5438 return false;
5439 }
5440
5441 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5442 * rights flags
5443 */
5444 return true;
5445}
5446
5447static bool tr_valid(struct kvm_vcpu *vcpu)
5448{
5449 struct kvm_segment tr;
5450
5451 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5452
Avi Kivity1872a3f2009-01-04 23:26:52 +02005453 if (tr.unusable)
5454 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005455 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005456 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005457 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005458 return false;
5459 if (!tr.present)
5460 return false;
5461
5462 return true;
5463}
5464
5465static bool ldtr_valid(struct kvm_vcpu *vcpu)
5466{
5467 struct kvm_segment ldtr;
5468
5469 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5470
Avi Kivity1872a3f2009-01-04 23:26:52 +02005471 if (ldtr.unusable)
5472 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005473 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005474 return false;
5475 if (ldtr.type != 2)
5476 return false;
5477 if (!ldtr.present)
5478 return false;
5479
5480 return true;
5481}
5482
5483static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5484{
5485 struct kvm_segment cs, ss;
5486
5487 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5488 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5489
Nadav Amitb32a9912015-03-29 16:33:04 +03005490 return ((cs.selector & SEGMENT_RPL_MASK) ==
5491 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005492}
5493
5494/*
5495 * Check if guest state is valid. Returns true if valid, false if
5496 * not.
5497 * We assume that registers are always usable
5498 */
5499static bool guest_state_valid(struct kvm_vcpu *vcpu)
5500{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005501 if (enable_unrestricted_guest)
5502 return true;
5503
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005504 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005505 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005506 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5507 return false;
5508 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5509 return false;
5510 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5511 return false;
5512 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5513 return false;
5514 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5515 return false;
5516 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5517 return false;
5518 } else {
5519 /* protected mode guest state checks */
5520 if (!cs_ss_rpl_check(vcpu))
5521 return false;
5522 if (!code_segment_valid(vcpu))
5523 return false;
5524 if (!stack_segment_valid(vcpu))
5525 return false;
5526 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5527 return false;
5528 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5529 return false;
5530 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5531 return false;
5532 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5533 return false;
5534 if (!tr_valid(vcpu))
5535 return false;
5536 if (!ldtr_valid(vcpu))
5537 return false;
5538 }
5539 /* TODO:
5540 * - Add checks on RIP
5541 * - Add checks on RFLAGS
5542 */
5543
5544 return true;
5545}
5546
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005547static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5548{
5549 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5550}
5551
Mike Dayd77c26f2007-10-08 09:02:08 -04005552static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005553{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005554 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005555 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005556 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005557
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005558 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005559 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005560 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5561 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005562 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005563 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005564 r = kvm_write_guest_page(kvm, fn++, &data,
5565 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005566 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005567 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005568 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5569 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005570 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005571 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5572 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005573 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005574 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005575 r = kvm_write_guest_page(kvm, fn, &data,
5576 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5577 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005578out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005579 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005580 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005581}
5582
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005583static int init_rmode_identity_map(struct kvm *kvm)
5584{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005585 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005586 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005587 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005588 u32 tmp;
5589
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005590 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005591 mutex_lock(&kvm->slots_lock);
5592
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005593 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005594 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005595
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005596 if (!kvm_vmx->ept_identity_map_addr)
5597 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5598 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005599
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005600 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005601 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005602 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005603 goto out2;
5604
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005605 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005606 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5607 if (r < 0)
5608 goto out;
5609 /* Set up identity-mapping pagetable for EPT in real mode */
5610 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5611 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5612 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5613 r = kvm_write_guest_page(kvm, identity_map_pfn,
5614 &tmp, i * sizeof(tmp), sizeof(tmp));
5615 if (r < 0)
5616 goto out;
5617 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005618 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005619
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005620out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005621 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005622
5623out2:
5624 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005625 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005626}
5627
Avi Kivity6aa8b732006-12-10 02:21:36 -08005628static void seg_setup(int seg)
5629{
Mathias Krause772e0312012-08-30 01:30:19 +02005630 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005631 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005632
5633 vmcs_write16(sf->selector, 0);
5634 vmcs_writel(sf->base, 0);
5635 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005636 ar = 0x93;
5637 if (seg == VCPU_SREG_CS)
5638 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005639
5640 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005641}
5642
Sheng Yangf78e0e22007-10-29 09:40:42 +08005643static int alloc_apic_access_page(struct kvm *kvm)
5644{
Xiao Guangrong44841412012-09-07 14:14:20 +08005645 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005646 int r = 0;
5647
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005648 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005649 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005650 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005651 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5652 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005653 if (r)
5654 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005655
Tang Chen73a6d942014-09-11 13:38:00 +08005656 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005657 if (is_error_page(page)) {
5658 r = -EFAULT;
5659 goto out;
5660 }
5661
Tang Chenc24ae0d2014-09-24 15:57:58 +08005662 /*
5663 * Do not pin the page in memory, so that memory hot-unplug
5664 * is able to migrate it.
5665 */
5666 put_page(page);
5667 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005668out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005669 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005670 return r;
5671}
5672
Wanpeng Li991e7a02015-09-16 17:30:05 +08005673static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005674{
5675 int vpid;
5676
Avi Kivity919818a2009-03-23 18:01:29 +02005677 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005678 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005679 spin_lock(&vmx_vpid_lock);
5680 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005681 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005682 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005683 else
5684 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005685 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005686 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005687}
5688
Wanpeng Li991e7a02015-09-16 17:30:05 +08005689static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005690{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005691 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005692 return;
5693 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005694 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005695 spin_unlock(&vmx_vpid_lock);
5696}
5697
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005698static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5699 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005700{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005701 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005702
5703 if (!cpu_has_vmx_msr_bitmap())
5704 return;
5705
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005706 if (static_branch_unlikely(&enable_evmcs))
5707 evmcs_touch_msr_bitmap();
5708
Sheng Yang25c5f222008-03-28 13:18:56 +08005709 /*
5710 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5711 * have the write-low and read-high bitmap offsets the wrong way round.
5712 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5713 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005714 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005715 if (type & MSR_TYPE_R)
5716 /* read-low */
5717 __clear_bit(msr, msr_bitmap + 0x000 / f);
5718
5719 if (type & MSR_TYPE_W)
5720 /* write-low */
5721 __clear_bit(msr, msr_bitmap + 0x800 / f);
5722
Sheng Yang25c5f222008-03-28 13:18:56 +08005723 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5724 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005725 if (type & MSR_TYPE_R)
5726 /* read-high */
5727 __clear_bit(msr, msr_bitmap + 0x400 / f);
5728
5729 if (type & MSR_TYPE_W)
5730 /* write-high */
5731 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5732
5733 }
5734}
5735
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005736static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5737 u32 msr, int type)
5738{
5739 int f = sizeof(unsigned long);
5740
5741 if (!cpu_has_vmx_msr_bitmap())
5742 return;
5743
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005744 if (static_branch_unlikely(&enable_evmcs))
5745 evmcs_touch_msr_bitmap();
5746
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005747 /*
5748 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5749 * have the write-low and read-high bitmap offsets the wrong way round.
5750 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5751 */
5752 if (msr <= 0x1fff) {
5753 if (type & MSR_TYPE_R)
5754 /* read-low */
5755 __set_bit(msr, msr_bitmap + 0x000 / f);
5756
5757 if (type & MSR_TYPE_W)
5758 /* write-low */
5759 __set_bit(msr, msr_bitmap + 0x800 / f);
5760
5761 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5762 msr &= 0x1fff;
5763 if (type & MSR_TYPE_R)
5764 /* read-high */
5765 __set_bit(msr, msr_bitmap + 0x400 / f);
5766
5767 if (type & MSR_TYPE_W)
5768 /* write-high */
5769 __set_bit(msr, msr_bitmap + 0xc00 / f);
5770
5771 }
5772}
5773
5774static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5775 u32 msr, int type, bool value)
5776{
5777 if (value)
5778 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5779 else
5780 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5781}
5782
Wincy Vanf2b93282015-02-03 23:56:03 +08005783/*
5784 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5785 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5786 */
5787static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5788 unsigned long *msr_bitmap_nested,
5789 u32 msr, int type)
5790{
5791 int f = sizeof(unsigned long);
5792
Wincy Vanf2b93282015-02-03 23:56:03 +08005793 /*
5794 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5795 * have the write-low and read-high bitmap offsets the wrong way round.
5796 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5797 */
5798 if (msr <= 0x1fff) {
5799 if (type & MSR_TYPE_R &&
5800 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5801 /* read-low */
5802 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5803
5804 if (type & MSR_TYPE_W &&
5805 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5806 /* write-low */
5807 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5808
5809 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5810 msr &= 0x1fff;
5811 if (type & MSR_TYPE_R &&
5812 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5813 /* read-high */
5814 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5815
5816 if (type & MSR_TYPE_W &&
5817 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5818 /* write-high */
5819 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5820
5821 }
5822}
5823
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005824static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005825{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005826 u8 mode = 0;
5827
5828 if (cpu_has_secondary_exec_ctrls() &&
5829 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5830 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5831 mode |= MSR_BITMAP_MODE_X2APIC;
5832 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5833 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5834 }
5835
5836 if (is_long_mode(vcpu))
5837 mode |= MSR_BITMAP_MODE_LM;
5838
5839 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005840}
5841
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005842#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5843
5844static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5845 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005846{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005847 int msr;
5848
5849 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5850 unsigned word = msr / BITS_PER_LONG;
5851 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5852 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005853 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005854
5855 if (mode & MSR_BITMAP_MODE_X2APIC) {
5856 /*
5857 * TPR reads and writes can be virtualized even if virtual interrupt
5858 * delivery is not in use.
5859 */
5860 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5861 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5862 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5863 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5864 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5865 }
5866 }
5867}
5868
5869static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5870{
5871 struct vcpu_vmx *vmx = to_vmx(vcpu);
5872 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5873 u8 mode = vmx_msr_bitmap_mode(vcpu);
5874 u8 changed = mode ^ vmx->msr_bitmap_mode;
5875
5876 if (!changed)
5877 return;
5878
5879 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5880 !(mode & MSR_BITMAP_MODE_LM));
5881
5882 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5883 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5884
5885 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005886}
5887
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005888static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005889{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005890 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005891}
5892
David Matlackc9f04402017-08-01 14:00:40 -07005893static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5894{
5895 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5896 gfn_t gfn;
5897
5898 /*
5899 * Don't need to mark the APIC access page dirty; it is never
5900 * written to by the CPU during APIC virtualization.
5901 */
5902
5903 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5904 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5905 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5906 }
5907
5908 if (nested_cpu_has_posted_intr(vmcs12)) {
5909 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5910 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5911 }
5912}
5913
5914
David Hildenbrand6342c502017-01-25 11:58:58 +01005915static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005916{
5917 struct vcpu_vmx *vmx = to_vmx(vcpu);
5918 int max_irr;
5919 void *vapic_page;
5920 u16 status;
5921
David Matlackc9f04402017-08-01 14:00:40 -07005922 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5923 return;
Wincy Van705699a2015-02-03 23:58:17 +08005924
David Matlackc9f04402017-08-01 14:00:40 -07005925 vmx->nested.pi_pending = false;
5926 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5927 return;
Wincy Van705699a2015-02-03 23:58:17 +08005928
David Matlackc9f04402017-08-01 14:00:40 -07005929 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5930 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005931 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005932 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5933 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005934 kunmap(vmx->nested.virtual_apic_page);
5935
5936 status = vmcs_read16(GUEST_INTR_STATUS);
5937 if ((u8)max_irr > ((u8)status & 0xff)) {
5938 status &= ~0xff;
5939 status |= (u8)max_irr;
5940 vmcs_write16(GUEST_INTR_STATUS, status);
5941 }
5942 }
David Matlackc9f04402017-08-01 14:00:40 -07005943
5944 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005945}
5946
Wincy Van06a55242017-04-28 13:13:59 +08005947static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5948 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005949{
5950#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005951 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5952
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005953 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005954 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005955 * The vector of interrupt to be delivered to vcpu had
5956 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005957 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005958 * Following cases will be reached in this block, and
5959 * we always send a notification event in all cases as
5960 * explained below.
5961 *
5962 * Case 1: vcpu keeps in non-root mode. Sending a
5963 * notification event posts the interrupt to vcpu.
5964 *
5965 * Case 2: vcpu exits to root mode and is still
5966 * runnable. PIR will be synced to vIRR before the
5967 * next vcpu entry. Sending a notification event in
5968 * this case has no effect, as vcpu is not in root
5969 * mode.
5970 *
5971 * Case 3: vcpu exits to root mode and is blocked.
5972 * vcpu_block() has already synced PIR to vIRR and
5973 * never blocks vcpu if vIRR is not cleared. Therefore,
5974 * a blocked vcpu here does not wait for any requested
5975 * interrupts in PIR, and sending a notification event
5976 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005977 */
Feng Wu28b835d2015-09-18 22:29:54 +08005978
Wincy Van06a55242017-04-28 13:13:59 +08005979 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005980 return true;
5981 }
5982#endif
5983 return false;
5984}
5985
Wincy Van705699a2015-02-03 23:58:17 +08005986static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5987 int vector)
5988{
5989 struct vcpu_vmx *vmx = to_vmx(vcpu);
5990
5991 if (is_guest_mode(vcpu) &&
5992 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005993 /*
5994 * If a posted intr is not recognized by hardware,
5995 * we will accomplish it in the next vmentry.
5996 */
5997 vmx->nested.pi_pending = true;
5998 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005999 /* the PIR and ON have been set by L1. */
6000 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6001 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006002 return 0;
6003 }
6004 return -1;
6005}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006006/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006007 * Send interrupt to vcpu via posted interrupt way.
6008 * 1. If target vcpu is running(non-root mode), send posted interrupt
6009 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6010 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6011 * interrupt from PIR in next vmentry.
6012 */
6013static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6014{
6015 struct vcpu_vmx *vmx = to_vmx(vcpu);
6016 int r;
6017
Wincy Van705699a2015-02-03 23:58:17 +08006018 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6019 if (!r)
6020 return;
6021
Yang Zhanga20ed542013-04-11 19:25:15 +08006022 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6023 return;
6024
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006025 /* If a previous notification has sent the IPI, nothing to do. */
6026 if (pi_test_and_set_on(&vmx->pi_desc))
6027 return;
6028
Wincy Van06a55242017-04-28 13:13:59 +08006029 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006030 kvm_vcpu_kick(vcpu);
6031}
6032
Avi Kivity6aa8b732006-12-10 02:21:36 -08006033/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006034 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6035 * will not change in the lifetime of the guest.
6036 * Note that host-state that does change is set elsewhere. E.g., host-state
6037 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6038 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006039static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006040{
6041 u32 low32, high32;
6042 unsigned long tmpl;
6043 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006044 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006045
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006046 cr0 = read_cr0();
6047 WARN_ON(cr0 & X86_CR0_TS);
6048 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006049
6050 /*
6051 * Save the most likely value for this task's CR3 in the VMCS.
6052 * We can't use __get_current_cr3_fast() because we're not atomic.
6053 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006054 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006055 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02006056 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006057
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006058 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006059 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006060 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02006061 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006062
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006063 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006064#ifdef CONFIG_X86_64
6065 /*
6066 * Load null selectors, so we can avoid reloading them in
6067 * __vmx_load_host_state(), in case userspace uses the null selectors
6068 * too (the expected case).
6069 */
6070 vmcs_write16(HOST_DS_SELECTOR, 0);
6071 vmcs_write16(HOST_ES_SELECTOR, 0);
6072#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006073 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6074 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006075#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006076 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6077 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6078
Juergen Gross87930012017-09-04 12:25:27 +02006079 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006080 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006081 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006082
Avi Kivity83287ea422012-09-16 15:10:57 +03006083 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006084
6085 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6086 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6087 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6088 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6089
6090 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6091 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6092 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6093 }
6094}
6095
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006096static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6097{
6098 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6099 if (enable_ept)
6100 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006101 if (is_guest_mode(&vmx->vcpu))
6102 vmx->vcpu.arch.cr4_guest_owned_bits &=
6103 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006104 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6105}
6106
Yang Zhang01e439b2013-04-11 19:25:12 +08006107static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6108{
6109 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6110
Andrey Smetanind62caab2015-11-10 15:36:33 +03006111 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006112 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006113
6114 if (!enable_vnmi)
6115 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6116
Yunhong Jiang64672c92016-06-13 14:19:59 -07006117 /* Enable the preemption timer dynamically */
6118 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006119 return pin_based_exec_ctrl;
6120}
6121
Andrey Smetanind62caab2015-11-10 15:36:33 +03006122static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6123{
6124 struct vcpu_vmx *vmx = to_vmx(vcpu);
6125
6126 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006127 if (cpu_has_secondary_exec_ctrls()) {
6128 if (kvm_vcpu_apicv_active(vcpu))
6129 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6130 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6131 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6132 else
6133 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6134 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6135 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6136 }
6137
6138 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006139 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006140}
6141
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006142static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6143{
6144 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006145
6146 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6147 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6148
Paolo Bonzini35754c92015-07-29 12:05:37 +02006149 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006150 exec_control &= ~CPU_BASED_TPR_SHADOW;
6151#ifdef CONFIG_X86_64
6152 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6153 CPU_BASED_CR8_LOAD_EXITING;
6154#endif
6155 }
6156 if (!enable_ept)
6157 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6158 CPU_BASED_CR3_LOAD_EXITING |
6159 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006160 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6161 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6162 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006163 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6164 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006165 return exec_control;
6166}
6167
Jim Mattson45ec3682017-08-23 16:32:04 -07006168static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006169{
Jim Mattson45ec3682017-08-23 16:32:04 -07006170 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006171 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006172}
6173
Jim Mattson75f4fc82017-08-23 16:32:03 -07006174static bool vmx_rdseed_supported(void)
6175{
6176 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006177 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006178}
6179
Paolo Bonzini80154d72017-08-24 13:55:35 +02006180static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006181{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006182 struct kvm_vcpu *vcpu = &vmx->vcpu;
6183
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006184 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006185
Paolo Bonzini80154d72017-08-24 13:55:35 +02006186 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006187 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6188 if (vmx->vpid == 0)
6189 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6190 if (!enable_ept) {
6191 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6192 enable_unrestricted_guest = 0;
6193 }
6194 if (!enable_unrestricted_guest)
6195 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006196 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006197 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006198 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006199 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6200 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006201 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006202
6203 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6204 * in vmx_set_cr4. */
6205 exec_control &= ~SECONDARY_EXEC_DESC;
6206
Abel Gordonabc4fc52013-04-18 14:35:25 +03006207 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6208 (handle_vmptrld).
6209 We can NOT enable shadow_vmcs here because we don't have yet
6210 a current VMCS12
6211 */
6212 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006213
6214 if (!enable_pml)
6215 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006216
Paolo Bonzini3db13482017-08-24 14:48:03 +02006217 if (vmx_xsaves_supported()) {
6218 /* Exposing XSAVES only when XSAVE is exposed */
6219 bool xsaves_enabled =
6220 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6221 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6222
6223 if (!xsaves_enabled)
6224 exec_control &= ~SECONDARY_EXEC_XSAVES;
6225
6226 if (nested) {
6227 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006228 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006229 SECONDARY_EXEC_XSAVES;
6230 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006231 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006232 ~SECONDARY_EXEC_XSAVES;
6233 }
6234 }
6235
Paolo Bonzini80154d72017-08-24 13:55:35 +02006236 if (vmx_rdtscp_supported()) {
6237 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6238 if (!rdtscp_enabled)
6239 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6240
6241 if (nested) {
6242 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006243 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006244 SECONDARY_EXEC_RDTSCP;
6245 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006246 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006247 ~SECONDARY_EXEC_RDTSCP;
6248 }
6249 }
6250
6251 if (vmx_invpcid_supported()) {
6252 /* Exposing INVPCID only when PCID is exposed */
6253 bool invpcid_enabled =
6254 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6255 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6256
6257 if (!invpcid_enabled) {
6258 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6259 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6260 }
6261
6262 if (nested) {
6263 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006264 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006265 SECONDARY_EXEC_ENABLE_INVPCID;
6266 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006267 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006268 ~SECONDARY_EXEC_ENABLE_INVPCID;
6269 }
6270 }
6271
Jim Mattson45ec3682017-08-23 16:32:04 -07006272 if (vmx_rdrand_supported()) {
6273 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6274 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006275 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006276
6277 if (nested) {
6278 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006279 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006280 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006281 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006282 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006283 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006284 }
6285 }
6286
Jim Mattson75f4fc82017-08-23 16:32:03 -07006287 if (vmx_rdseed_supported()) {
6288 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6289 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006290 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006291
6292 if (nested) {
6293 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006294 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006295 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006296 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006297 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006298 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006299 }
6300 }
6301
Paolo Bonzini80154d72017-08-24 13:55:35 +02006302 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006303}
6304
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006305static void ept_set_mmio_spte_mask(void)
6306{
6307 /*
6308 * EPT Misconfigurations can be generated if the value of bits 2:0
6309 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006310 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006311 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6312 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006313}
6314
Wanpeng Lif53cd632014-12-02 19:14:58 +08006315#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006316/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006317 * Sets up the vmcs for emulated real mode.
6318 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006319static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006320{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006321#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006322 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006323#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006324 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006325
Abel Gordon4607c2d2013-04-18 14:35:55 +03006326 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006327 /*
6328 * At vCPU creation, "VMWRITE to any supported field
6329 * in the VMCS" is supported, so use the more
6330 * permissive vmx_vmread_bitmap to specify both read
6331 * and write permissions for the shadow VMCS.
6332 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006333 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006334 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006335 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006336 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006337 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006338
Avi Kivity6aa8b732006-12-10 02:21:36 -08006339 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6340
Avi Kivity6aa8b732006-12-10 02:21:36 -08006341 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006342 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006343 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006344
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006345 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006346
Dan Williamsdfa169b2016-06-02 11:17:24 -07006347 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006348 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006349 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006350 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006351 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006352
Andrey Smetanind62caab2015-11-10 15:36:33 +03006353 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006354 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6355 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6356 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6357 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6358
6359 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006360
Li RongQing0bcf2612015-12-03 13:29:34 +08006361 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006362 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006363 }
6364
Wanpeng Lib31c1142018-03-12 04:53:04 -07006365 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006366 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006367 vmx->ple_window = ple_window;
6368 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006369 }
6370
Xiao Guangrongc3707952011-07-12 03:28:04 +08006371 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6372 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006373 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6374
Avi Kivity9581d442010-10-19 16:46:55 +02006375 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6376 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006377 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006378#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006379 rdmsrl(MSR_FS_BASE, a);
6380 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6381 rdmsrl(MSR_GS_BASE, a);
6382 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6383#else
6384 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6385 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6386#endif
6387
Bandan Das2a499e42017-08-03 15:54:41 -04006388 if (cpu_has_vmx_vmfunc())
6389 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6390
Eddie Dong2cc51562007-05-21 07:28:09 +03006391 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6392 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006393 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006394 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006395 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006396
Radim Krčmář74545702015-04-27 15:11:25 +02006397 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6398 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006399
Paolo Bonzini03916db2014-07-24 14:21:57 +02006400 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006401 u32 index = vmx_msr_index[i];
6402 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006403 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006404
6405 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6406 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006407 if (wrmsr_safe(index, data_low, data_high) < 0)
6408 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006409 vmx->guest_msrs[j].index = i;
6410 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006411 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006412 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006413 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006414
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006415 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6416 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006417
6418 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006419
6420 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006421 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006422
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006423 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6424 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6425
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006426 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006427
Wanpeng Lif53cd632014-12-02 19:14:58 +08006428 if (vmx_xsaves_supported())
6429 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6430
Peter Feiner4e595162016-07-07 14:49:58 -07006431 if (enable_pml) {
6432 ASSERT(vmx->pml_pg);
6433 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6434 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6435 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006436}
6437
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006438static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006439{
6440 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006441 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006442 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006443
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006444 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006445 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006446
Wanpeng Li518e7b92018-02-28 14:03:31 +08006447 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006448 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006449 kvm_set_cr8(vcpu, 0);
6450
6451 if (!init_event) {
6452 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6453 MSR_IA32_APICBASE_ENABLE;
6454 if (kvm_vcpu_is_reset_bsp(vcpu))
6455 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6456 apic_base_msr.host_initiated = true;
6457 kvm_set_apic_base(vcpu, &apic_base_msr);
6458 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006459
Avi Kivity2fb92db2011-04-27 19:42:18 +03006460 vmx_segment_cache_clear(vmx);
6461
Avi Kivity5706be02008-08-20 15:07:31 +03006462 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006463 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006464 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006465
6466 seg_setup(VCPU_SREG_DS);
6467 seg_setup(VCPU_SREG_ES);
6468 seg_setup(VCPU_SREG_FS);
6469 seg_setup(VCPU_SREG_GS);
6470 seg_setup(VCPU_SREG_SS);
6471
6472 vmcs_write16(GUEST_TR_SELECTOR, 0);
6473 vmcs_writel(GUEST_TR_BASE, 0);
6474 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6475 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6476
6477 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6478 vmcs_writel(GUEST_LDTR_BASE, 0);
6479 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6480 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6481
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006482 if (!init_event) {
6483 vmcs_write32(GUEST_SYSENTER_CS, 0);
6484 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6485 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6486 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6487 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006488
Wanpeng Lic37c2872017-11-20 14:52:21 -08006489 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006490 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006491
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006492 vmcs_writel(GUEST_GDTR_BASE, 0);
6493 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6494
6495 vmcs_writel(GUEST_IDTR_BASE, 0);
6496 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6497
Anthony Liguori443381a2010-12-06 10:53:38 -06006498 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006499 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006500 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006501 if (kvm_mpx_supported())
6502 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006503
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006504 setup_msrs(vmx);
6505
Avi Kivity6aa8b732006-12-10 02:21:36 -08006506 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6507
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006508 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006509 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006510 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006511 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006512 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006513 vmcs_write32(TPR_THRESHOLD, 0);
6514 }
6515
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006516 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006517
Sheng Yang2384d2b2008-01-17 15:14:33 +08006518 if (vmx->vpid != 0)
6519 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6520
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006521 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006522 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006523 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006524 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006525 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006526
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006527 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006528
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006529 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006530 if (init_event)
6531 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006532}
6533
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006534/*
6535 * In nested virtualization, check if L1 asked to exit on external interrupts.
6536 * For most existing hypervisors, this will always return true.
6537 */
6538static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6539{
6540 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6541 PIN_BASED_EXT_INTR_MASK;
6542}
6543
Bandan Das77b0f5d2014-04-19 18:17:45 -04006544/*
6545 * In nested virtualization, check if L1 has set
6546 * VM_EXIT_ACK_INTR_ON_EXIT
6547 */
6548static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6549{
6550 return get_vmcs12(vcpu)->vm_exit_controls &
6551 VM_EXIT_ACK_INTR_ON_EXIT;
6552}
6553
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006554static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6555{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006556 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006557}
6558
Jan Kiszkac9a79532014-03-07 20:03:15 +01006559static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006560{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006561 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6562 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006563}
6564
Jan Kiszkac9a79532014-03-07 20:03:15 +01006565static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006566{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006567 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006568 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006569 enable_irq_window(vcpu);
6570 return;
6571 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006572
Paolo Bonzini47c01522016-12-19 11:44:07 +01006573 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6574 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006575}
6576
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006577static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006578{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006579 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006580 uint32_t intr;
6581 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006582
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006583 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006584
Avi Kivityfa89a812008-09-01 15:57:51 +03006585 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006586 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006587 int inc_eip = 0;
6588 if (vcpu->arch.interrupt.soft)
6589 inc_eip = vcpu->arch.event_exit_inst_len;
6590 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006591 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006592 return;
6593 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006594 intr = irq | INTR_INFO_VALID_MASK;
6595 if (vcpu->arch.interrupt.soft) {
6596 intr |= INTR_TYPE_SOFT_INTR;
6597 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6598 vmx->vcpu.arch.event_exit_inst_len);
6599 } else
6600 intr |= INTR_TYPE_EXT_INTR;
6601 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006602
6603 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006604}
6605
Sheng Yangf08864b2008-05-15 18:23:25 +08006606static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6607{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006608 struct vcpu_vmx *vmx = to_vmx(vcpu);
6609
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006610 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006611 /*
6612 * Tracking the NMI-blocked state in software is built upon
6613 * finding the next open IRQ window. This, in turn, depends on
6614 * well-behaving guests: They have to keep IRQs disabled at
6615 * least as long as the NMI handler runs. Otherwise we may
6616 * cause NMI nesting, maybe breaking the guest. But as this is
6617 * highly unlikely, we can live with the residual risk.
6618 */
6619 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6620 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6621 }
6622
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006623 ++vcpu->stat.nmi_injections;
6624 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006625
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006626 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006627 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006628 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006629 return;
6630 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006631
Sheng Yangf08864b2008-05-15 18:23:25 +08006632 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6633 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006634
6635 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006636}
6637
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006638static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6639{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006640 struct vcpu_vmx *vmx = to_vmx(vcpu);
6641 bool masked;
6642
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006643 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006644 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006645 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006646 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006647 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6648 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6649 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006650}
6651
6652static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6653{
6654 struct vcpu_vmx *vmx = to_vmx(vcpu);
6655
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006656 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006657 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6658 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6659 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6660 }
6661 } else {
6662 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6663 if (masked)
6664 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6665 GUEST_INTR_STATE_NMI);
6666 else
6667 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6668 GUEST_INTR_STATE_NMI);
6669 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006670}
6671
Jan Kiszka2505dc92013-04-14 12:12:47 +02006672static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6673{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006674 if (to_vmx(vcpu)->nested.nested_run_pending)
6675 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006676
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006677 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006678 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6679 return 0;
6680
Jan Kiszka2505dc92013-04-14 12:12:47 +02006681 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6682 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6683 | GUEST_INTR_STATE_NMI));
6684}
6685
Gleb Natapov78646122009-03-23 12:12:11 +02006686static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6687{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006688 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6689 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006690 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6691 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006692}
6693
Izik Eiduscbc94022007-10-25 00:29:55 +02006694static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6695{
6696 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006697
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006698 if (enable_unrestricted_guest)
6699 return 0;
6700
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006701 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6702 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006703 if (ret)
6704 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006705 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006706 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006707}
6708
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006709static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6710{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006711 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006712 return 0;
6713}
6714
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006715static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006716{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006717 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006718 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006719 /*
6720 * Update instruction length as we may reinject the exception
6721 * from user space while in guest debugging mode.
6722 */
6723 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6724 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006725 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006726 return false;
6727 /* fall through */
6728 case DB_VECTOR:
6729 if (vcpu->guest_debug &
6730 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6731 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006732 /* fall through */
6733 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006734 case OF_VECTOR:
6735 case BR_VECTOR:
6736 case UD_VECTOR:
6737 case DF_VECTOR:
6738 case SS_VECTOR:
6739 case GP_VECTOR:
6740 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006741 return true;
6742 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006743 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006744 return false;
6745}
6746
6747static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6748 int vec, u32 err_code)
6749{
6750 /*
6751 * Instruction with address size override prefix opcode 0x67
6752 * Cause the #SS fault with 0 error code in VM86 mode.
6753 */
6754 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6755 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6756 if (vcpu->arch.halt_request) {
6757 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006758 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006759 }
6760 return 1;
6761 }
6762 return 0;
6763 }
6764
6765 /*
6766 * Forward all other exceptions that are valid in real mode.
6767 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6768 * the required debugging infrastructure rework.
6769 */
6770 kvm_queue_exception(vcpu, vec);
6771 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006772}
6773
Andi Kleena0861c02009-06-08 17:37:09 +08006774/*
6775 * Trigger machine check on the host. We assume all the MSRs are already set up
6776 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6777 * We pass a fake environment to the machine check handler because we want
6778 * the guest to be always treated like user space, no matter what context
6779 * it used internally.
6780 */
6781static void kvm_machine_check(void)
6782{
6783#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6784 struct pt_regs regs = {
6785 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6786 .flags = X86_EFLAGS_IF,
6787 };
6788
6789 do_machine_check(&regs, 0);
6790#endif
6791}
6792
Avi Kivity851ba692009-08-24 11:10:17 +03006793static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006794{
6795 /* already handled by vcpu_run */
6796 return 1;
6797}
6798
Avi Kivity851ba692009-08-24 11:10:17 +03006799static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006800{
Avi Kivity1155f762007-11-22 11:30:47 +02006801 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006802 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006803 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006804 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006805 u32 vect_info;
6806 enum emulation_result er;
6807
Avi Kivity1155f762007-11-22 11:30:47 +02006808 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006809 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006810
Andi Kleena0861c02009-06-08 17:37:09 +08006811 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006812 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006813
Jim Mattsonef85b672016-12-12 11:01:37 -08006814 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006815 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006816
Wanpeng Li082d06e2018-04-03 16:28:48 -07006817 if (is_invalid_opcode(intr_info))
6818 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006819
Avi Kivity6aa8b732006-12-10 02:21:36 -08006820 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006821 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006822 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006823
Liran Alon9e869482018-03-12 13:12:51 +02006824 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6825 WARN_ON_ONCE(!enable_vmware_backdoor);
6826 er = emulate_instruction(vcpu,
6827 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6828 if (er == EMULATE_USER_EXIT)
6829 return 0;
6830 else if (er != EMULATE_DONE)
6831 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6832 return 1;
6833 }
6834
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006835 /*
6836 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6837 * MMIO, it is better to report an internal error.
6838 * See the comments in vmx_handle_exit.
6839 */
6840 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6841 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6842 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6843 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006844 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006845 vcpu->run->internal.data[0] = vect_info;
6846 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006847 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006848 return 0;
6849 }
6850
Avi Kivity6aa8b732006-12-10 02:21:36 -08006851 if (is_page_fault(intr_info)) {
6852 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006853 /* EPT won't cause page fault directly */
6854 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006855 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006856 }
6857
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006858 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006859
6860 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6861 return handle_rmode_exception(vcpu, ex_no, error_code);
6862
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006863 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006864 case AC_VECTOR:
6865 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6866 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006867 case DB_VECTOR:
6868 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6869 if (!(vcpu->guest_debug &
6870 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006871 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006872 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006873 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006874 skip_emulated_instruction(vcpu);
6875
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006876 kvm_queue_exception(vcpu, DB_VECTOR);
6877 return 1;
6878 }
6879 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6880 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6881 /* fall through */
6882 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006883 /*
6884 * Update instruction length as we may reinject #BP from
6885 * user space while in guest debugging mode. Reading it for
6886 * #DB as well causes no harm, it is not used in that case.
6887 */
6888 vmx->vcpu.arch.event_exit_inst_len =
6889 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006890 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006891 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006892 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6893 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006894 break;
6895 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006896 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6897 kvm_run->ex.exception = ex_no;
6898 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006899 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006900 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006901 return 0;
6902}
6903
Avi Kivity851ba692009-08-24 11:10:17 +03006904static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006905{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006906 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006907 return 1;
6908}
6909
Avi Kivity851ba692009-08-24 11:10:17 +03006910static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006911{
Avi Kivity851ba692009-08-24 11:10:17 +03006912 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006913 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006914 return 0;
6915}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006916
Avi Kivity851ba692009-08-24 11:10:17 +03006917static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006918{
He, Qingbfdaab02007-09-12 14:18:28 +08006919 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006920 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006921 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006922
He, Qingbfdaab02007-09-12 14:18:28 +08006923 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006924 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006925
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006926 ++vcpu->stat.io_exits;
6927
Sean Christopherson432baf62018-03-08 08:57:26 -08006928 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006929 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006930
6931 port = exit_qualification >> 16;
6932 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006933 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006934
Sean Christophersondca7f122018-03-08 08:57:27 -08006935 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006936}
6937
Ingo Molnar102d8322007-02-19 14:37:47 +02006938static void
6939vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6940{
6941 /*
6942 * Patch in the VMCALL instruction:
6943 */
6944 hypercall[0] = 0x0f;
6945 hypercall[1] = 0x01;
6946 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006947}
6948
Guo Chao0fa06072012-06-28 15:16:19 +08006949/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006950static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6951{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006952 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006953 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6954 unsigned long orig_val = val;
6955
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006956 /*
6957 * We get here when L2 changed cr0 in a way that did not change
6958 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006959 * but did change L0 shadowed bits. So we first calculate the
6960 * effective cr0 value that L1 would like to write into the
6961 * hardware. It consists of the L2-owned bits from the new
6962 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006963 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006964 val = (val & ~vmcs12->cr0_guest_host_mask) |
6965 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6966
David Matlack38991522016-11-29 18:14:08 -08006967 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006968 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006969
6970 if (kvm_set_cr0(vcpu, val))
6971 return 1;
6972 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006973 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006974 } else {
6975 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006976 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006977 return 1;
David Matlack38991522016-11-29 18:14:08 -08006978
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006979 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006980 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006981}
6982
6983static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6984{
6985 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006986 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6987 unsigned long orig_val = val;
6988
6989 /* analogously to handle_set_cr0 */
6990 val = (val & ~vmcs12->cr4_guest_host_mask) |
6991 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6992 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006993 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006994 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006995 return 0;
6996 } else
6997 return kvm_set_cr4(vcpu, val);
6998}
6999
Paolo Bonzini0367f202016-07-12 10:44:55 +02007000static int handle_desc(struct kvm_vcpu *vcpu)
7001{
7002 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7003 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7004}
7005
Avi Kivity851ba692009-08-24 11:10:17 +03007006static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007007{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007008 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007009 int cr;
7010 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007011 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007012 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007013
He, Qingbfdaab02007-09-12 14:18:28 +08007014 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007015 cr = exit_qualification & 15;
7016 reg = (exit_qualification >> 8) & 15;
7017 switch ((exit_qualification >> 4) & 3) {
7018 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007019 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007020 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007021 switch (cr) {
7022 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007023 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007024 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007025 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007026 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007027 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007028 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007029 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007030 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007031 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007032 case 8: {
7033 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007034 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007035 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007036 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007037 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007038 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007039 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007040 return ret;
7041 /*
7042 * TODO: we might be squashing a
7043 * KVM_GUESTDBG_SINGLESTEP-triggered
7044 * KVM_EXIT_DEBUG here.
7045 */
Avi Kivity851ba692009-08-24 11:10:17 +03007046 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007047 return 0;
7048 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007049 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007050 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007051 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007052 WARN_ONCE(1, "Guest should always own CR0.TS");
7053 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007054 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007055 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007056 case 1: /*mov from cr*/
7057 switch (cr) {
7058 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007059 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007060 val = kvm_read_cr3(vcpu);
7061 kvm_register_write(vcpu, reg, val);
7062 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007063 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007064 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007065 val = kvm_get_cr8(vcpu);
7066 kvm_register_write(vcpu, reg, val);
7067 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007068 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007069 }
7070 break;
7071 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007072 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007073 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007074 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007075
Kyle Huey6affcbe2016-11-29 12:40:40 -08007076 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007077 default:
7078 break;
7079 }
Avi Kivity851ba692009-08-24 11:10:17 +03007080 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007081 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007082 (int)(exit_qualification >> 4) & 3, cr);
7083 return 0;
7084}
7085
Avi Kivity851ba692009-08-24 11:10:17 +03007086static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007087{
He, Qingbfdaab02007-09-12 14:18:28 +08007088 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007089 int dr, dr7, reg;
7090
7091 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7092 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7093
7094 /* First, if DR does not exist, trigger UD */
7095 if (!kvm_require_dr(vcpu, dr))
7096 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007097
Jan Kiszkaf2483412010-01-20 18:20:20 +01007098 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007099 if (!kvm_require_cpl(vcpu, 0))
7100 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007101 dr7 = vmcs_readl(GUEST_DR7);
7102 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007103 /*
7104 * As the vm-exit takes precedence over the debug trap, we
7105 * need to emulate the latter, either for the host or the
7106 * guest debugging itself.
7107 */
7108 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007109 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007110 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007111 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007112 vcpu->run->debug.arch.exception = DB_VECTOR;
7113 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007114 return 0;
7115 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007116 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007117 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007118 kvm_queue_exception(vcpu, DB_VECTOR);
7119 return 1;
7120 }
7121 }
7122
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007123 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007124 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7125 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007126
7127 /*
7128 * No more DR vmexits; force a reload of the debug registers
7129 * and reenter on this instruction. The next vmexit will
7130 * retrieve the full state of the debug registers.
7131 */
7132 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7133 return 1;
7134 }
7135
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007136 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7137 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007138 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007139
7140 if (kvm_get_dr(vcpu, dr, &val))
7141 return 1;
7142 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007143 } else
Nadav Amit57773922014-06-18 17:19:23 +03007144 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007145 return 1;
7146
Kyle Huey6affcbe2016-11-29 12:40:40 -08007147 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007148}
7149
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007150static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7151{
7152 return vcpu->arch.dr6;
7153}
7154
7155static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7156{
7157}
7158
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007159static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7160{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007161 get_debugreg(vcpu->arch.db[0], 0);
7162 get_debugreg(vcpu->arch.db[1], 1);
7163 get_debugreg(vcpu->arch.db[2], 2);
7164 get_debugreg(vcpu->arch.db[3], 3);
7165 get_debugreg(vcpu->arch.dr6, 6);
7166 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7167
7168 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007169 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007170}
7171
Gleb Natapov020df072010-04-13 10:05:23 +03007172static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7173{
7174 vmcs_writel(GUEST_DR7, val);
7175}
7176
Avi Kivity851ba692009-08-24 11:10:17 +03007177static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007178{
Kyle Huey6a908b62016-11-29 12:40:37 -08007179 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007180}
7181
Avi Kivity851ba692009-08-24 11:10:17 +03007182static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007183{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007184 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007185 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007186
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007187 msr_info.index = ecx;
7188 msr_info.host_initiated = false;
7189 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007190 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007191 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007192 return 1;
7193 }
7194
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007195 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007196
Avi Kivity6aa8b732006-12-10 02:21:36 -08007197 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007198 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7199 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007200 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007201}
7202
Avi Kivity851ba692009-08-24 11:10:17 +03007203static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007204{
Will Auld8fe8ab42012-11-29 12:42:12 -08007205 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007206 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7207 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7208 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007209
Will Auld8fe8ab42012-11-29 12:42:12 -08007210 msr.data = data;
7211 msr.index = ecx;
7212 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007213 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007214 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007215 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007216 return 1;
7217 }
7218
Avi Kivity59200272010-01-25 19:47:02 +02007219 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007220 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007221}
7222
Avi Kivity851ba692009-08-24 11:10:17 +03007223static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007224{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007225 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007226 return 1;
7227}
7228
Avi Kivity851ba692009-08-24 11:10:17 +03007229static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007230{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007231 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7232 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007233
Avi Kivity3842d132010-07-27 12:30:24 +03007234 kvm_make_request(KVM_REQ_EVENT, vcpu);
7235
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007236 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007237 return 1;
7238}
7239
Avi Kivity851ba692009-08-24 11:10:17 +03007240static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007241{
Avi Kivityd3bef152007-06-05 15:53:05 +03007242 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007243}
7244
Avi Kivity851ba692009-08-24 11:10:17 +03007245static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007246{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007247 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007248}
7249
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007250static int handle_invd(struct kvm_vcpu *vcpu)
7251{
Andre Przywara51d8b662010-12-21 11:12:02 +01007252 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007253}
7254
Avi Kivity851ba692009-08-24 11:10:17 +03007255static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007256{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007257 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007258
7259 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007260 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007261}
7262
Avi Kivityfee84b02011-11-10 14:57:25 +02007263static int handle_rdpmc(struct kvm_vcpu *vcpu)
7264{
7265 int err;
7266
7267 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007268 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007269}
7270
Avi Kivity851ba692009-08-24 11:10:17 +03007271static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007272{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007273 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007274}
7275
Dexuan Cui2acf9232010-06-10 11:27:12 +08007276static int handle_xsetbv(struct kvm_vcpu *vcpu)
7277{
7278 u64 new_bv = kvm_read_edx_eax(vcpu);
7279 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7280
7281 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007282 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007283 return 1;
7284}
7285
Wanpeng Lif53cd632014-12-02 19:14:58 +08007286static int handle_xsaves(struct kvm_vcpu *vcpu)
7287{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007288 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007289 WARN(1, "this should never happen\n");
7290 return 1;
7291}
7292
7293static int handle_xrstors(struct kvm_vcpu *vcpu)
7294{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007295 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007296 WARN(1, "this should never happen\n");
7297 return 1;
7298}
7299
Avi Kivity851ba692009-08-24 11:10:17 +03007300static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007301{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007302 if (likely(fasteoi)) {
7303 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7304 int access_type, offset;
7305
7306 access_type = exit_qualification & APIC_ACCESS_TYPE;
7307 offset = exit_qualification & APIC_ACCESS_OFFSET;
7308 /*
7309 * Sane guest uses MOV to write EOI, with written value
7310 * not cared. So make a short-circuit here by avoiding
7311 * heavy instruction emulation.
7312 */
7313 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7314 (offset == APIC_EOI)) {
7315 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007316 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007317 }
7318 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007319 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007320}
7321
Yang Zhangc7c9c562013-01-25 10:18:51 +08007322static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7323{
7324 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7325 int vector = exit_qualification & 0xff;
7326
7327 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7328 kvm_apic_set_eoi_accelerated(vcpu, vector);
7329 return 1;
7330}
7331
Yang Zhang83d4c282013-01-25 10:18:49 +08007332static int handle_apic_write(struct kvm_vcpu *vcpu)
7333{
7334 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7335 u32 offset = exit_qualification & 0xfff;
7336
7337 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7338 kvm_apic_write_nodecode(vcpu, offset);
7339 return 1;
7340}
7341
Avi Kivity851ba692009-08-24 11:10:17 +03007342static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007343{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007344 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007345 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007346 bool has_error_code = false;
7347 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007348 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007349 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007350
7351 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007352 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007353 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007354
7355 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7356
7357 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007358 if (reason == TASK_SWITCH_GATE && idt_v) {
7359 switch (type) {
7360 case INTR_TYPE_NMI_INTR:
7361 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007362 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007363 break;
7364 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007365 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007366 kvm_clear_interrupt_queue(vcpu);
7367 break;
7368 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007369 if (vmx->idt_vectoring_info &
7370 VECTORING_INFO_DELIVER_CODE_MASK) {
7371 has_error_code = true;
7372 error_code =
7373 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7374 }
7375 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007376 case INTR_TYPE_SOFT_EXCEPTION:
7377 kvm_clear_exception_queue(vcpu);
7378 break;
7379 default:
7380 break;
7381 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007382 }
Izik Eidus37817f22008-03-24 23:14:53 +02007383 tss_selector = exit_qualification;
7384
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007385 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7386 type != INTR_TYPE_EXT_INTR &&
7387 type != INTR_TYPE_NMI_INTR))
7388 skip_emulated_instruction(vcpu);
7389
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007390 if (kvm_task_switch(vcpu, tss_selector,
7391 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7392 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007393 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7394 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7395 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007396 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007397 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007398
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007399 /*
7400 * TODO: What about debug traps on tss switch?
7401 * Are we supposed to inject them and update dr6?
7402 */
7403
7404 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007405}
7406
Avi Kivity851ba692009-08-24 11:10:17 +03007407static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007408{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007409 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007410 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007411 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007412
Sheng Yangf9c617f2009-03-25 10:08:52 +08007413 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007414
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007415 /*
7416 * EPT violation happened while executing iret from NMI,
7417 * "blocked by NMI" bit has to be set before next VM entry.
7418 * There are errata that may cause this bit to not be set:
7419 * AAK134, BY25.
7420 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007421 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007422 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007423 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007424 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7425
Sheng Yang14394422008-04-28 12:24:45 +08007426 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007427 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007428
Junaid Shahid27959a42016-12-06 16:46:10 -08007429 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007430 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007431 ? PFERR_USER_MASK : 0;
7432 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007433 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007434 ? PFERR_WRITE_MASK : 0;
7435 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007436 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007437 ? PFERR_FETCH_MASK : 0;
7438 /* ept page table entry is present? */
7439 error_code |= (exit_qualification &
7440 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7441 EPT_VIOLATION_EXECUTABLE))
7442 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007443
Paolo Bonzinieebed242016-11-28 14:39:58 +01007444 error_code |= (exit_qualification & 0x100) != 0 ?
7445 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007446
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007447 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007448 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007449}
7450
Avi Kivity851ba692009-08-24 11:10:17 +03007451static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007452{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007453 gpa_t gpa;
7454
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007455 /*
7456 * A nested guest cannot optimize MMIO vmexits, because we have an
7457 * nGPA here instead of the required GPA.
7458 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007459 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007460 if (!is_guest_mode(vcpu) &&
7461 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007462 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007463 /*
7464 * Doing kvm_skip_emulated_instruction() depends on undefined
7465 * behavior: Intel's manual doesn't mandate
7466 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7467 * occurs and while on real hardware it was observed to be set,
7468 * other hypervisors (namely Hyper-V) don't set it, we end up
7469 * advancing IP with some random value. Disable fast mmio when
7470 * running nested and keep it for real hardware in hope that
7471 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7472 */
7473 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7474 return kvm_skip_emulated_instruction(vcpu);
7475 else
7476 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7477 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007478 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007479
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007480 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007481}
7482
Avi Kivity851ba692009-08-24 11:10:17 +03007483static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007484{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007485 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007486 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7487 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007488 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007489 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007490
7491 return 1;
7492}
7493
Mohammed Gamal80ced182009-09-01 12:48:18 +02007494static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007495{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007496 struct vcpu_vmx *vmx = to_vmx(vcpu);
7497 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007498 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007499 u32 cpu_exec_ctrl;
7500 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007501 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007502
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007503 /*
7504 * We should never reach the point where we are emulating L2
7505 * due to invalid guest state as that means we incorrectly
7506 * allowed a nested VMEntry with an invalid vmcs12.
7507 */
7508 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7509
Avi Kivity49e9d552010-09-19 14:34:08 +02007510 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7511 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007512
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007513 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007514 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007515 return handle_interrupt_window(&vmx->vcpu);
7516
Radim Krčmář72875d82017-04-26 22:32:19 +02007517 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007518 return 1;
7519
Liran Alon9b8ae632017-11-05 16:56:34 +02007520 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007521
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007522 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007523 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007524 ret = 0;
7525 goto out;
7526 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007527
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007528 if (err != EMULATE_DONE)
7529 goto emulation_error;
7530
7531 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7532 vcpu->arch.exception.pending)
7533 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007534
Gleb Natapov8d76c492013-05-08 18:38:44 +03007535 if (vcpu->arch.halt_request) {
7536 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007537 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007538 goto out;
7539 }
7540
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007541 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007542 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007543 if (need_resched())
7544 schedule();
7545 }
7546
Mohammed Gamal80ced182009-09-01 12:48:18 +02007547out:
7548 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007549
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007550emulation_error:
7551 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7552 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7553 vcpu->run->internal.ndata = 0;
7554 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007555}
7556
7557static void grow_ple_window(struct kvm_vcpu *vcpu)
7558{
7559 struct vcpu_vmx *vmx = to_vmx(vcpu);
7560 int old = vmx->ple_window;
7561
Babu Mogerc8e88712018-03-16 16:37:24 -04007562 vmx->ple_window = __grow_ple_window(old, ple_window,
7563 ple_window_grow,
7564 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007565
7566 if (vmx->ple_window != old)
7567 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007568
7569 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007570}
7571
7572static void shrink_ple_window(struct kvm_vcpu *vcpu)
7573{
7574 struct vcpu_vmx *vmx = to_vmx(vcpu);
7575 int old = vmx->ple_window;
7576
Babu Mogerc8e88712018-03-16 16:37:24 -04007577 vmx->ple_window = __shrink_ple_window(old, ple_window,
7578 ple_window_shrink,
7579 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007580
7581 if (vmx->ple_window != old)
7582 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007583
7584 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007585}
7586
7587/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007588 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7589 */
7590static void wakeup_handler(void)
7591{
7592 struct kvm_vcpu *vcpu;
7593 int cpu = smp_processor_id();
7594
7595 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7596 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7597 blocked_vcpu_list) {
7598 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7599
7600 if (pi_test_on(pi_desc) == 1)
7601 kvm_vcpu_kick(vcpu);
7602 }
7603 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7604}
7605
Peng Haoe01bca22018-04-07 05:47:32 +08007606static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007607{
7608 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7609 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7610 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7611 0ull, VMX_EPT_EXECUTABLE_MASK,
7612 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007613 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007614
7615 ept_set_mmio_spte_mask();
7616 kvm_enable_tdp();
7617}
7618
Tiejun Chenf2c76482014-10-28 10:14:47 +08007619static __init int hardware_setup(void)
7620{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007621 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007622 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007623
7624 rdmsrl_safe(MSR_EFER, &host_efer);
7625
7626 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7627 kvm_define_shared_msr(i, vmx_msr_index[i]);
7628
Radim Krčmář23611332016-09-29 22:41:33 +02007629 for (i = 0; i < VMX_BITMAP_NR; i++) {
7630 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7631 if (!vmx_bitmap[i])
7632 goto out;
7633 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007634
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007635 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7636 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7637
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007638 if (setup_vmcs_config(&vmcs_config) < 0) {
7639 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007640 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007641 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007642
7643 if (boot_cpu_has(X86_FEATURE_NX))
7644 kvm_enable_efer_bits(EFER_NX);
7645
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007646 if (boot_cpu_has(X86_FEATURE_MPX)) {
7647 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7648 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7649 }
7650
Wanpeng Li08d839c2017-03-23 05:30:08 -07007651 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7652 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007653 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007654
Tiejun Chenf2c76482014-10-28 10:14:47 +08007655 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007656 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007657 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007658 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007659 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007660
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007661 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007662 enable_ept_ad_bits = 0;
7663
Wanpeng Li8ad81822017-10-09 15:51:53 -07007664 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007665 enable_unrestricted_guest = 0;
7666
Paolo Bonziniad15a292015-01-30 16:18:49 +01007667 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007668 flexpriority_enabled = 0;
7669
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007670 if (!cpu_has_virtual_nmis())
7671 enable_vnmi = 0;
7672
Paolo Bonziniad15a292015-01-30 16:18:49 +01007673 /*
7674 * set_apic_access_page_addr() is used to reload apic access
7675 * page upon invalidation. No need to do anything if not
7676 * using the APIC_ACCESS_ADDR VMCS field.
7677 */
7678 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007679 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007680
7681 if (!cpu_has_vmx_tpr_shadow())
7682 kvm_x86_ops->update_cr8_intercept = NULL;
7683
7684 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7685 kvm_disable_largepages();
7686
Tianyu Lan877ad952018-07-19 08:40:23 +00007687#if IS_ENABLED(CONFIG_HYPERV)
7688 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7689 && enable_ept)
7690 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7691#endif
7692
Wanpeng Li0f107682017-09-28 18:06:24 -07007693 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007694 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007695 ple_window = 0;
7696 ple_window_grow = 0;
7697 ple_window_max = 0;
7698 ple_window_shrink = 0;
7699 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007700
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007701 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007702 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007703 kvm_x86_ops->sync_pir_to_irr = NULL;
7704 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007705
Haozhong Zhang64903d62015-10-20 15:39:09 +08007706 if (cpu_has_vmx_tsc_scaling()) {
7707 kvm_has_tsc_control = true;
7708 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7709 kvm_tsc_scaling_ratio_frac_bits = 48;
7710 }
7711
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007712 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7713
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007714 if (enable_ept)
7715 vmx_enable_tdp();
7716 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007717 kvm_disable_tdp();
7718
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007719 if (!nested) {
7720 kvm_x86_ops->get_nested_state = NULL;
7721 kvm_x86_ops->set_nested_state = NULL;
7722 }
7723
Kai Huang843e4332015-01-28 10:54:28 +08007724 /*
7725 * Only enable PML when hardware supports PML feature, and both EPT
7726 * and EPT A/D bit features are enabled -- PML depends on them to work.
7727 */
7728 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7729 enable_pml = 0;
7730
7731 if (!enable_pml) {
7732 kvm_x86_ops->slot_enable_log_dirty = NULL;
7733 kvm_x86_ops->slot_disable_log_dirty = NULL;
7734 kvm_x86_ops->flush_log_dirty = NULL;
7735 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7736 }
7737
Yunhong Jiang64672c92016-06-13 14:19:59 -07007738 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7739 u64 vmx_msr;
7740
7741 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7742 cpu_preemption_timer_multi =
7743 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7744 } else {
7745 kvm_x86_ops->set_hv_timer = NULL;
7746 kvm_x86_ops->cancel_hv_timer = NULL;
7747 }
7748
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007749 if (!cpu_has_vmx_shadow_vmcs())
7750 enable_shadow_vmcs = 0;
7751 if (enable_shadow_vmcs)
7752 init_vmcs_shadow_fields();
7753
Feng Wubf9f6ac2015-09-18 22:29:55 +08007754 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007755 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007756
Ashok Rajc45dcc72016-06-22 14:59:56 +08007757 kvm_mce_cap_supported |= MCG_LMCE_P;
7758
Tiejun Chenf2c76482014-10-28 10:14:47 +08007759 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007760
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007761out:
Radim Krčmář23611332016-09-29 22:41:33 +02007762 for (i = 0; i < VMX_BITMAP_NR; i++)
7763 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007764
7765 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007766}
7767
7768static __exit void hardware_unsetup(void)
7769{
Radim Krčmář23611332016-09-29 22:41:33 +02007770 int i;
7771
7772 for (i = 0; i < VMX_BITMAP_NR; i++)
7773 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007774
Tiejun Chenf2c76482014-10-28 10:14:47 +08007775 free_kvm_area();
7776}
7777
Avi Kivity6aa8b732006-12-10 02:21:36 -08007778/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007779 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7780 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7781 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007782static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007783{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007784 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007785 grow_ple_window(vcpu);
7786
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007787 /*
7788 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7789 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7790 * never set PAUSE_EXITING and just set PLE if supported,
7791 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7792 */
7793 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007794 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007795}
7796
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007797static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007798{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007799 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007800}
7801
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007802static int handle_mwait(struct kvm_vcpu *vcpu)
7803{
7804 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7805 return handle_nop(vcpu);
7806}
7807
Jim Mattson45ec3682017-08-23 16:32:04 -07007808static int handle_invalid_op(struct kvm_vcpu *vcpu)
7809{
7810 kvm_queue_exception(vcpu, UD_VECTOR);
7811 return 1;
7812}
7813
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007814static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7815{
7816 return 1;
7817}
7818
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007819static int handle_monitor(struct kvm_vcpu *vcpu)
7820{
7821 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7822 return handle_nop(vcpu);
7823}
7824
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007825/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007826 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7827 * set the success or error code of an emulated VMX instruction, as specified
7828 * by Vol 2B, VMX Instruction Reference, "Conventions".
7829 */
7830static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7831{
7832 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7833 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7834 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7835}
7836
7837static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7838{
7839 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7840 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7841 X86_EFLAGS_SF | X86_EFLAGS_OF))
7842 | X86_EFLAGS_CF);
7843}
7844
Abel Gordon145c28d2013-04-18 14:36:55 +03007845static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007846 u32 vm_instruction_error)
7847{
7848 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7849 /*
7850 * failValid writes the error number to the current VMCS, which
7851 * can't be done there isn't a current VMCS.
7852 */
7853 nested_vmx_failInvalid(vcpu);
7854 return;
7855 }
7856 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7857 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7858 X86_EFLAGS_SF | X86_EFLAGS_OF))
7859 | X86_EFLAGS_ZF);
7860 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7861 /*
7862 * We don't need to force a shadow sync because
7863 * VM_INSTRUCTION_ERROR is not shadowed
7864 */
7865}
Abel Gordon145c28d2013-04-18 14:36:55 +03007866
Wincy Vanff651cb2014-12-11 08:52:58 +03007867static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7868{
7869 /* TODO: not to reset guest simply here. */
7870 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007871 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007872}
7873
Jan Kiszkaf4124502014-03-07 20:03:13 +01007874static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7875{
7876 struct vcpu_vmx *vmx =
7877 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7878
7879 vmx->nested.preemption_timer_expired = true;
7880 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7881 kvm_vcpu_kick(&vmx->vcpu);
7882
7883 return HRTIMER_NORESTART;
7884}
7885
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007886/*
Bandan Das19677e32014-05-06 02:19:15 -04007887 * Decode the memory-address operand of a vmx instruction, as recorded on an
7888 * exit caused by such an instruction (run by a guest hypervisor).
7889 * On success, returns 0. When the operand is invalid, returns 1 and throws
7890 * #UD or #GP.
7891 */
7892static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7893 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007894 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007895{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007896 gva_t off;
7897 bool exn;
7898 struct kvm_segment s;
7899
Bandan Das19677e32014-05-06 02:19:15 -04007900 /*
7901 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7902 * Execution", on an exit, vmx_instruction_info holds most of the
7903 * addressing components of the operand. Only the displacement part
7904 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7905 * For how an actual address is calculated from all these components,
7906 * refer to Vol. 1, "Operand Addressing".
7907 */
7908 int scaling = vmx_instruction_info & 3;
7909 int addr_size = (vmx_instruction_info >> 7) & 7;
7910 bool is_reg = vmx_instruction_info & (1u << 10);
7911 int seg_reg = (vmx_instruction_info >> 15) & 7;
7912 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7913 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7914 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7915 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7916
7917 if (is_reg) {
7918 kvm_queue_exception(vcpu, UD_VECTOR);
7919 return 1;
7920 }
7921
7922 /* Addr = segment_base + offset */
7923 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007924 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007925 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007926 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007927 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007928 off += kvm_register_read(vcpu, index_reg)<<scaling;
7929 vmx_get_segment(vcpu, &s, seg_reg);
7930 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007931
7932 if (addr_size == 1) /* 32 bit */
7933 *ret &= 0xffffffff;
7934
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007935 /* Checks for #GP/#SS exceptions. */
7936 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007937 if (is_long_mode(vcpu)) {
7938 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7939 * non-canonical form. This is the only check on the memory
7940 * destination for long mode!
7941 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007942 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007943 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007944 /* Protected mode: apply checks for segment validity in the
7945 * following order:
7946 * - segment type check (#GP(0) may be thrown)
7947 * - usability check (#GP(0)/#SS(0))
7948 * - limit check (#GP(0)/#SS(0))
7949 */
7950 if (wr)
7951 /* #GP(0) if the destination operand is located in a
7952 * read-only data segment or any code segment.
7953 */
7954 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7955 else
7956 /* #GP(0) if the source operand is located in an
7957 * execute-only code segment
7958 */
7959 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007960 if (exn) {
7961 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7962 return 1;
7963 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007964 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7965 */
7966 exn = (s.unusable != 0);
7967 /* Protected mode: #GP(0)/#SS(0) if the memory
7968 * operand is outside the segment limit.
7969 */
7970 exn = exn || (off + sizeof(u64) > s.limit);
7971 }
7972 if (exn) {
7973 kvm_queue_exception_e(vcpu,
7974 seg_reg == VCPU_SREG_SS ?
7975 SS_VECTOR : GP_VECTOR,
7976 0);
7977 return 1;
7978 }
7979
Bandan Das19677e32014-05-06 02:19:15 -04007980 return 0;
7981}
7982
Radim Krčmářcbf71272017-05-19 15:48:51 +02007983static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007984{
7985 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007986 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007987
7988 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007989 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007990 return 1;
7991
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007992 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007993 kvm_inject_page_fault(vcpu, &e);
7994 return 1;
7995 }
7996
Bandan Das3573e222014-05-06 02:19:16 -04007997 return 0;
7998}
7999
Liran Alonabfc52c2018-06-23 02:35:13 +03008000/*
8001 * Allocate a shadow VMCS and associate it with the currently loaded
8002 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8003 * VMCS is also VMCLEARed, so that it is ready for use.
8004 */
8005static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8006{
8007 struct vcpu_vmx *vmx = to_vmx(vcpu);
8008 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8009
8010 /*
8011 * We should allocate a shadow vmcs for vmcs01 only when L1
8012 * executes VMXON and free it when L1 executes VMXOFF.
8013 * As it is invalid to execute VMXON twice, we shouldn't reach
8014 * here when vmcs01 already have an allocated shadow vmcs.
8015 */
8016 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8017
8018 if (!loaded_vmcs->shadow_vmcs) {
8019 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8020 if (loaded_vmcs->shadow_vmcs)
8021 vmcs_clear(loaded_vmcs->shadow_vmcs);
8022 }
8023 return loaded_vmcs->shadow_vmcs;
8024}
8025
Jim Mattsone29acc52016-11-30 12:03:43 -08008026static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8027{
8028 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008029 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008030
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008031 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8032 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008033 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008034
8035 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8036 if (!vmx->nested.cached_vmcs12)
8037 goto out_cached_vmcs12;
8038
Liran Alon61ada742018-06-23 02:35:08 +03008039 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8040 if (!vmx->nested.cached_shadow_vmcs12)
8041 goto out_cached_shadow_vmcs12;
8042
Liran Alonabfc52c2018-06-23 02:35:13 +03008043 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8044 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008045
Jim Mattsone29acc52016-11-30 12:03:43 -08008046 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8047 HRTIMER_MODE_REL_PINNED);
8048 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8049
8050 vmx->nested.vmxon = true;
8051 return 0;
8052
8053out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008054 kfree(vmx->nested.cached_shadow_vmcs12);
8055
8056out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008057 kfree(vmx->nested.cached_vmcs12);
8058
8059out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008060 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008061
Jim Mattsonde3a0022017-11-27 17:22:25 -06008062out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008063 return -ENOMEM;
8064}
8065
Bandan Das3573e222014-05-06 02:19:16 -04008066/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008067 * Emulate the VMXON instruction.
8068 * Currently, we just remember that VMX is active, and do not save or even
8069 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8070 * do not currently need to store anything in that guest-allocated memory
8071 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8072 * argument is different from the VMXON pointer (which the spec says they do).
8073 */
8074static int handle_vmon(struct kvm_vcpu *vcpu)
8075{
Jim Mattsone29acc52016-11-30 12:03:43 -08008076 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008077 gpa_t vmptr;
8078 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008079 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008080 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8081 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008082
Jim Mattson70f3aac2017-04-26 08:53:46 -07008083 /*
8084 * The Intel VMX Instruction Reference lists a bunch of bits that are
8085 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8086 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8087 * Otherwise, we should fail with #UD. But most faulting conditions
8088 * have already been checked by hardware, prior to the VM-exit for
8089 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8090 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008091 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008092 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008093 kvm_queue_exception(vcpu, UD_VECTOR);
8094 return 1;
8095 }
8096
Felix Wilhelm727ba742018-06-11 09:43:44 +02008097 /* CPL=0 must be checked manually. */
8098 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008099 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008100 return 1;
8101 }
8102
Abel Gordon145c28d2013-04-18 14:36:55 +03008103 if (vmx->nested.vmxon) {
8104 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008105 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008106 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008107
Haozhong Zhang3b840802016-06-22 14:59:54 +08008108 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008109 != VMXON_NEEDED_FEATURES) {
8110 kvm_inject_gp(vcpu, 0);
8111 return 1;
8112 }
8113
Radim Krčmářcbf71272017-05-19 15:48:51 +02008114 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008115 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008116
8117 /*
8118 * SDM 3: 24.11.5
8119 * The first 4 bytes of VMXON region contain the supported
8120 * VMCS revision identifier
8121 *
8122 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8123 * which replaces physical address width with 32
8124 */
8125 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8126 nested_vmx_failInvalid(vcpu);
8127 return kvm_skip_emulated_instruction(vcpu);
8128 }
8129
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008130 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8131 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008132 nested_vmx_failInvalid(vcpu);
8133 return kvm_skip_emulated_instruction(vcpu);
8134 }
8135 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8136 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008137 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008138 nested_vmx_failInvalid(vcpu);
8139 return kvm_skip_emulated_instruction(vcpu);
8140 }
8141 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008142 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008143
8144 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008145 ret = enter_vmx_operation(vcpu);
8146 if (ret)
8147 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008148
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008149 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008150 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008151}
8152
8153/*
8154 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8155 * for running VMX instructions (except VMXON, whose prerequisites are
8156 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008157 * Note that many of these exceptions have priority over VM exits, so they
8158 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008159 */
8160static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8161{
Jim Mattsone49fcb82018-07-27 13:44:45 -07008162 if (!to_vmx(vcpu)->nested.vmxon) {
8163 kvm_queue_exception(vcpu, UD_VECTOR);
8164 return 0;
8165 }
8166
Felix Wilhelm727ba742018-06-11 09:43:44 +02008167 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008168 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008169 return 0;
8170 }
8171
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008172 return 1;
8173}
8174
David Matlack8ca44e82017-08-01 14:00:39 -07008175static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8176{
8177 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8178 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8179}
8180
Abel Gordone7953d72013-04-18 14:37:55 +03008181static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8182{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008183 if (vmx->nested.current_vmptr == -1ull)
8184 return;
8185
Abel Gordon012f83c2013-04-18 14:39:25 +03008186 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008187 /* copy to memory all shadowed fields in case
8188 they were modified */
8189 copy_shadow_to_vmcs12(vmx);
8190 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008191 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008192 }
Wincy Van705699a2015-02-03 23:58:17 +08008193 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008194
8195 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008196 kvm_vcpu_write_guest_page(&vmx->vcpu,
8197 vmx->nested.current_vmptr >> PAGE_SHIFT,
8198 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008199
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008200 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008201}
8202
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008203/*
8204 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8205 * just stops using VMX.
8206 */
8207static void free_nested(struct vcpu_vmx *vmx)
8208{
Wanpeng Lib7455822017-11-22 14:04:00 -08008209 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008210 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008211
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008212 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008213 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008214 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008215 vmx->nested.posted_intr_nv = -1;
8216 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008217 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008218 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008219 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8220 free_vmcs(vmx->vmcs01.shadow_vmcs);
8221 vmx->vmcs01.shadow_vmcs = NULL;
8222 }
David Matlack4f2777b2016-07-13 17:16:37 -07008223 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008224 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008225 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008226 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008227 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008228 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008229 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008230 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008231 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008232 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008233 }
Wincy Van705699a2015-02-03 23:58:17 +08008234 if (vmx->nested.pi_desc_page) {
8235 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008236 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008237 vmx->nested.pi_desc_page = NULL;
8238 vmx->nested.pi_desc = NULL;
8239 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008240
Jim Mattsonde3a0022017-11-27 17:22:25 -06008241 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008242}
8243
8244/* Emulate the VMXOFF instruction */
8245static int handle_vmoff(struct kvm_vcpu *vcpu)
8246{
8247 if (!nested_vmx_check_permission(vcpu))
8248 return 1;
8249 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008250 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008251 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008252}
8253
Nadav Har'El27d6c862011-05-25 23:06:59 +03008254/* Emulate the VMCLEAR instruction */
8255static int handle_vmclear(struct kvm_vcpu *vcpu)
8256{
8257 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008258 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008259 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008260
8261 if (!nested_vmx_check_permission(vcpu))
8262 return 1;
8263
Radim Krčmářcbf71272017-05-19 15:48:51 +02008264 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008265 return 1;
8266
Radim Krčmářcbf71272017-05-19 15:48:51 +02008267 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8268 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8269 return kvm_skip_emulated_instruction(vcpu);
8270 }
8271
8272 if (vmptr == vmx->nested.vmxon_ptr) {
8273 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8274 return kvm_skip_emulated_instruction(vcpu);
8275 }
8276
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008277 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008278 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008279
Jim Mattson587d7e722017-03-02 12:41:48 -08008280 kvm_vcpu_write_guest(vcpu,
8281 vmptr + offsetof(struct vmcs12, launch_state),
8282 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008283
Nadav Har'El27d6c862011-05-25 23:06:59 +03008284 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008285 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008286}
8287
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008288static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8289
8290/* Emulate the VMLAUNCH instruction */
8291static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8292{
8293 return nested_vmx_run(vcpu, true);
8294}
8295
8296/* Emulate the VMRESUME instruction */
8297static int handle_vmresume(struct kvm_vcpu *vcpu)
8298{
8299
8300 return nested_vmx_run(vcpu, false);
8301}
8302
Nadav Har'El49f705c2011-05-25 23:08:30 +03008303/*
8304 * Read a vmcs12 field. Since these can have varying lengths and we return
8305 * one type, we chose the biggest type (u64) and zero-extend the return value
8306 * to that size. Note that the caller, handle_vmread, might need to use only
8307 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8308 * 64-bit fields are to be returned).
8309 */
Liran Alone2536742018-06-23 02:35:02 +03008310static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008311 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008312{
8313 short offset = vmcs_field_to_offset(field);
8314 char *p;
8315
8316 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008317 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008318
Liran Alone2536742018-06-23 02:35:02 +03008319 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008320
Jim Mattsond37f4262017-12-22 12:12:16 -08008321 switch (vmcs_field_width(field)) {
8322 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008323 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008324 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008325 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008326 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008327 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008328 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008329 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008330 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008331 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008332 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008333 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008334 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008335 WARN_ON(1);
8336 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008337 }
8338}
8339
Abel Gordon20b97fe2013-04-18 14:36:25 +03008340
Liran Alone2536742018-06-23 02:35:02 +03008341static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008342 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008343 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008344 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008345 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008346 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008347
Jim Mattsond37f4262017-12-22 12:12:16 -08008348 switch (vmcs_field_width(field)) {
8349 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008350 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008351 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008352 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008353 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008354 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008355 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008356 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008357 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008358 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008359 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008360 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008361 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008362 WARN_ON(1);
8363 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008364 }
8365
8366}
8367
Jim Mattsonf4160e42018-05-29 09:11:33 -07008368/*
8369 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8370 * they have been modified by the L1 guest. Note that the "read-only"
8371 * VM-exit information fields are actually writable if the vCPU is
8372 * configured to support "VMWRITE to any supported field in the VMCS."
8373 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008374static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8375{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008376 const u16 *fields[] = {
8377 shadow_read_write_fields,
8378 shadow_read_only_fields
8379 };
8380 const int max_fields[] = {
8381 max_shadow_read_write_fields,
8382 max_shadow_read_only_fields
8383 };
8384 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008385 unsigned long field;
8386 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008387 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008388
Jan Kiszka282da872014-10-08 18:05:39 +02008389 preempt_disable();
8390
Abel Gordon16f5b902013-04-18 14:38:25 +03008391 vmcs_load(shadow_vmcs);
8392
Jim Mattsonf4160e42018-05-29 09:11:33 -07008393 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8394 for (i = 0; i < max_fields[q]; i++) {
8395 field = fields[q][i];
8396 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008397 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008398 }
8399 /*
8400 * Skip the VM-exit information fields if they are read-only.
8401 */
8402 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8403 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008404 }
8405
8406 vmcs_clear(shadow_vmcs);
8407 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008408
8409 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008410}
8411
Abel Gordonc3114422013-04-18 14:38:55 +03008412static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8413{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008414 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008415 shadow_read_write_fields,
8416 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008417 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008418 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008419 max_shadow_read_write_fields,
8420 max_shadow_read_only_fields
8421 };
8422 int i, q;
8423 unsigned long field;
8424 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008425 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008426
8427 vmcs_load(shadow_vmcs);
8428
Mathias Krausec2bae892013-06-26 20:36:21 +02008429 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008430 for (i = 0; i < max_fields[q]; i++) {
8431 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008432 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008433 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008434 }
8435 }
8436
8437 vmcs_clear(shadow_vmcs);
8438 vmcs_load(vmx->loaded_vmcs->vmcs);
8439}
8440
Nadav Har'El49f705c2011-05-25 23:08:30 +03008441/*
8442 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8443 * used before) all generate the same failure when it is missing.
8444 */
8445static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8446{
8447 struct vcpu_vmx *vmx = to_vmx(vcpu);
8448 if (vmx->nested.current_vmptr == -1ull) {
8449 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008450 return 0;
8451 }
8452 return 1;
8453}
8454
8455static int handle_vmread(struct kvm_vcpu *vcpu)
8456{
8457 unsigned long field;
8458 u64 field_value;
8459 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8460 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8461 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008462 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008463
Kyle Hueyeb277562016-11-29 12:40:39 -08008464 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008465 return 1;
8466
Kyle Huey6affcbe2016-11-29 12:40:40 -08008467 if (!nested_vmx_check_vmcs12(vcpu))
8468 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008469
Liran Alon6d894f42018-06-23 02:35:09 +03008470 if (!is_guest_mode(vcpu))
8471 vmcs12 = get_vmcs12(vcpu);
8472 else {
8473 /*
8474 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8475 * to shadowed-field sets the ALU flags for VMfailInvalid.
8476 */
8477 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8478 nested_vmx_failInvalid(vcpu);
8479 return kvm_skip_emulated_instruction(vcpu);
8480 }
8481 vmcs12 = get_shadow_vmcs12(vcpu);
8482 }
8483
Nadav Har'El49f705c2011-05-25 23:08:30 +03008484 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008485 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008486 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008487 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008488 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008489 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008490 }
8491 /*
8492 * Now copy part of this value to register or memory, as requested.
8493 * Note that the number of bits actually copied is 32 or 64 depending
8494 * on the guest's mode (32 or 64 bit), not on the given field's length.
8495 */
8496 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008497 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008498 field_value);
8499 } else {
8500 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008501 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008502 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008503 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008504 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8505 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008506 }
8507
8508 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008509 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008510}
8511
8512
8513static int handle_vmwrite(struct kvm_vcpu *vcpu)
8514{
8515 unsigned long field;
8516 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008517 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008518 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8519 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008520
Nadav Har'El49f705c2011-05-25 23:08:30 +03008521 /* The value to write might be 32 or 64 bits, depending on L1's long
8522 * mode, and eventually we need to write that into a field of several
8523 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008524 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008525 * bits into the vmcs12 field.
8526 */
8527 u64 field_value = 0;
8528 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008529 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008530
Kyle Hueyeb277562016-11-29 12:40:39 -08008531 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008532 return 1;
8533
Kyle Huey6affcbe2016-11-29 12:40:40 -08008534 if (!nested_vmx_check_vmcs12(vcpu))
8535 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008536
Nadav Har'El49f705c2011-05-25 23:08:30 +03008537 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008538 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008539 (((vmx_instruction_info) >> 3) & 0xf));
8540 else {
8541 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008542 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008543 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008544 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8545 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008546 kvm_inject_page_fault(vcpu, &e);
8547 return 1;
8548 }
8549 }
8550
8551
Nadav Amit27e6fb52014-06-18 17:19:26 +03008552 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008553 /*
8554 * If the vCPU supports "VMWRITE to any supported field in the
8555 * VMCS," then the "read-only" fields are actually read/write.
8556 */
8557 if (vmcs_field_readonly(field) &&
8558 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008559 nested_vmx_failValid(vcpu,
8560 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008561 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008562 }
8563
Liran Alon6d894f42018-06-23 02:35:09 +03008564 if (!is_guest_mode(vcpu))
8565 vmcs12 = get_vmcs12(vcpu);
8566 else {
8567 /*
8568 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8569 * to shadowed-field sets the ALU flags for VMfailInvalid.
8570 */
8571 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8572 nested_vmx_failInvalid(vcpu);
8573 return kvm_skip_emulated_instruction(vcpu);
8574 }
8575 vmcs12 = get_shadow_vmcs12(vcpu);
8576
8577 }
8578
8579 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008580 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008581 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008582 }
8583
Liran Alon6d894f42018-06-23 02:35:09 +03008584 /*
8585 * Do not track vmcs12 dirty-state if in guest-mode
8586 * as we actually dirty shadow vmcs12 instead of vmcs12.
8587 */
8588 if (!is_guest_mode(vcpu)) {
8589 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008590#define SHADOW_FIELD_RW(x) case x:
8591#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008592 /*
8593 * The fields that can be updated by L1 without a vmexit are
8594 * always updated in the vmcs02, the others go down the slow
8595 * path of prepare_vmcs02.
8596 */
8597 break;
8598 default:
8599 vmx->nested.dirty_vmcs12 = true;
8600 break;
8601 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008602 }
8603
Nadav Har'El49f705c2011-05-25 23:08:30 +03008604 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008605 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008606}
8607
Jim Mattsona8bc2842016-11-30 12:03:44 -08008608static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8609{
8610 vmx->nested.current_vmptr = vmptr;
8611 if (enable_shadow_vmcs) {
8612 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8613 SECONDARY_EXEC_SHADOW_VMCS);
8614 vmcs_write64(VMCS_LINK_POINTER,
8615 __pa(vmx->vmcs01.shadow_vmcs));
8616 vmx->nested.sync_shadow_vmcs = true;
8617 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008618 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008619}
8620
Nadav Har'El63846662011-05-25 23:07:29 +03008621/* Emulate the VMPTRLD instruction */
8622static int handle_vmptrld(struct kvm_vcpu *vcpu)
8623{
8624 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008625 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008626
8627 if (!nested_vmx_check_permission(vcpu))
8628 return 1;
8629
Radim Krčmářcbf71272017-05-19 15:48:51 +02008630 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008631 return 1;
8632
Radim Krčmářcbf71272017-05-19 15:48:51 +02008633 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8634 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8635 return kvm_skip_emulated_instruction(vcpu);
8636 }
8637
8638 if (vmptr == vmx->nested.vmxon_ptr) {
8639 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8640 return kvm_skip_emulated_instruction(vcpu);
8641 }
8642
Nadav Har'El63846662011-05-25 23:07:29 +03008643 if (vmx->nested.current_vmptr != vmptr) {
8644 struct vmcs12 *new_vmcs12;
8645 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008646 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8647 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008648 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008649 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008650 }
8651 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008652 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008653 (new_vmcs12->hdr.shadow_vmcs &&
8654 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008655 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008656 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008657 nested_vmx_failValid(vcpu,
8658 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008659 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008660 }
Nadav Har'El63846662011-05-25 23:07:29 +03008661
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008662 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008663 /*
8664 * Load VMCS12 from guest memory since it is not already
8665 * cached.
8666 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008667 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8668 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008669 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008670
Jim Mattsona8bc2842016-11-30 12:03:44 -08008671 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008672 }
8673
8674 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008675 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008676}
8677
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008678/* Emulate the VMPTRST instruction */
8679static int handle_vmptrst(struct kvm_vcpu *vcpu)
8680{
8681 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8682 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8683 gva_t vmcs_gva;
8684 struct x86_exception e;
8685
8686 if (!nested_vmx_check_permission(vcpu))
8687 return 1;
8688
8689 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008690 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008691 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008692 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008693 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8694 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8695 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008696 kvm_inject_page_fault(vcpu, &e);
8697 return 1;
8698 }
8699 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008700 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008701}
8702
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008703/* Emulate the INVEPT instruction */
8704static int handle_invept(struct kvm_vcpu *vcpu)
8705{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008706 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008707 u32 vmx_instruction_info, types;
8708 unsigned long type;
8709 gva_t gva;
8710 struct x86_exception e;
8711 struct {
8712 u64 eptp, gpa;
8713 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008714
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008715 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008716 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008717 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008718 kvm_queue_exception(vcpu, UD_VECTOR);
8719 return 1;
8720 }
8721
8722 if (!nested_vmx_check_permission(vcpu))
8723 return 1;
8724
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008725 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008726 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008727
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008728 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008729
Jim Mattson85c856b2016-10-26 08:38:38 -07008730 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008731 nested_vmx_failValid(vcpu,
8732 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008733 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008734 }
8735
8736 /* According to the Intel VMX instruction reference, the memory
8737 * operand is read even if it isn't needed (e.g., for type==global)
8738 */
8739 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008740 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008741 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008742 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008743 kvm_inject_page_fault(vcpu, &e);
8744 return 1;
8745 }
8746
8747 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008748 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008749 /*
8750 * TODO: track mappings and invalidate
8751 * single context requests appropriately
8752 */
8753 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008754 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008755 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008756 nested_vmx_succeed(vcpu);
8757 break;
8758 default:
8759 BUG_ON(1);
8760 break;
8761 }
8762
Kyle Huey6affcbe2016-11-29 12:40:40 -08008763 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008764}
8765
Petr Matouseka642fc32014-09-23 20:22:30 +02008766static int handle_invvpid(struct kvm_vcpu *vcpu)
8767{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008768 struct vcpu_vmx *vmx = to_vmx(vcpu);
8769 u32 vmx_instruction_info;
8770 unsigned long type, types;
8771 gva_t gva;
8772 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008773 struct {
8774 u64 vpid;
8775 u64 gla;
8776 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008777
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008778 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008779 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008780 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008781 kvm_queue_exception(vcpu, UD_VECTOR);
8782 return 1;
8783 }
8784
8785 if (!nested_vmx_check_permission(vcpu))
8786 return 1;
8787
8788 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8789 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8790
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008791 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008792 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008793
Jim Mattson85c856b2016-10-26 08:38:38 -07008794 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008795 nested_vmx_failValid(vcpu,
8796 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008797 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008798 }
8799
8800 /* according to the intel vmx instruction reference, the memory
8801 * operand is read even if it isn't needed (e.g., for type==global)
8802 */
8803 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8804 vmx_instruction_info, false, &gva))
8805 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008806 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008807 kvm_inject_page_fault(vcpu, &e);
8808 return 1;
8809 }
Jim Mattson40352602017-06-28 09:37:37 -07008810 if (operand.vpid >> 16) {
8811 nested_vmx_failValid(vcpu,
8812 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8813 return kvm_skip_emulated_instruction(vcpu);
8814 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008815
8816 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008817 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008818 if (!operand.vpid ||
8819 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008820 nested_vmx_failValid(vcpu,
8821 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8822 return kvm_skip_emulated_instruction(vcpu);
8823 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008824 if (cpu_has_vmx_invvpid_individual_addr() &&
8825 vmx->nested.vpid02) {
8826 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8827 vmx->nested.vpid02, operand.gla);
8828 } else
8829 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8830 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008831 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008832 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008833 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008834 nested_vmx_failValid(vcpu,
8835 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008836 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008837 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008838 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008839 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008840 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008841 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008842 break;
8843 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008844 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008845 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008846 }
8847
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008848 nested_vmx_succeed(vcpu);
8849
Kyle Huey6affcbe2016-11-29 12:40:40 -08008850 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008851}
8852
Junaid Shahideb4b2482018-06-27 14:59:14 -07008853static int handle_invpcid(struct kvm_vcpu *vcpu)
8854{
8855 u32 vmx_instruction_info;
8856 unsigned long type;
8857 bool pcid_enabled;
8858 gva_t gva;
8859 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07008860 unsigned i;
8861 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07008862 struct {
8863 u64 pcid;
8864 u64 gla;
8865 } operand;
8866
8867 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
8868 kvm_queue_exception(vcpu, UD_VECTOR);
8869 return 1;
8870 }
8871
8872 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8873 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8874
8875 if (type > 3) {
8876 kvm_inject_gp(vcpu, 0);
8877 return 1;
8878 }
8879
8880 /* According to the Intel instruction reference, the memory operand
8881 * is read even if it isn't needed (e.g., for type==all)
8882 */
8883 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8884 vmx_instruction_info, false, &gva))
8885 return 1;
8886
8887 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8888 kvm_inject_page_fault(vcpu, &e);
8889 return 1;
8890 }
8891
8892 if (operand.pcid >> 12 != 0) {
8893 kvm_inject_gp(vcpu, 0);
8894 return 1;
8895 }
8896
8897 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
8898
8899 switch (type) {
8900 case INVPCID_TYPE_INDIV_ADDR:
8901 if ((!pcid_enabled && (operand.pcid != 0)) ||
8902 is_noncanonical_address(operand.gla, vcpu)) {
8903 kvm_inject_gp(vcpu, 0);
8904 return 1;
8905 }
8906 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
8907 return kvm_skip_emulated_instruction(vcpu);
8908
8909 case INVPCID_TYPE_SINGLE_CTXT:
8910 if (!pcid_enabled && (operand.pcid != 0)) {
8911 kvm_inject_gp(vcpu, 0);
8912 return 1;
8913 }
8914
8915 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
8916 kvm_mmu_sync_roots(vcpu);
8917 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8918 }
8919
Junaid Shahidb94742c2018-06-27 14:59:20 -07008920 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
8921 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
8922 == operand.pcid)
8923 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07008924
Junaid Shahidb94742c2018-06-27 14:59:20 -07008925 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07008926 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07008927 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07008928 * given PCID, then nothing needs to be done here because a
8929 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07008930 */
8931
8932 return kvm_skip_emulated_instruction(vcpu);
8933
8934 case INVPCID_TYPE_ALL_NON_GLOBAL:
8935 /*
8936 * Currently, KVM doesn't mark global entries in the shadow
8937 * page tables, so a non-global flush just degenerates to a
8938 * global flush. If needed, we could optimize this later by
8939 * keeping track of global entries in shadow page tables.
8940 */
8941
8942 /* fall-through */
8943 case INVPCID_TYPE_ALL_INCL_GLOBAL:
8944 kvm_mmu_unload(vcpu);
8945 return kvm_skip_emulated_instruction(vcpu);
8946
8947 default:
8948 BUG(); /* We have already checked above that type <= 3 */
8949 }
8950}
8951
Kai Huang843e4332015-01-28 10:54:28 +08008952static int handle_pml_full(struct kvm_vcpu *vcpu)
8953{
8954 unsigned long exit_qualification;
8955
8956 trace_kvm_pml_full(vcpu->vcpu_id);
8957
8958 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8959
8960 /*
8961 * PML buffer FULL happened while executing iret from NMI,
8962 * "blocked by NMI" bit has to be set before next VM entry.
8963 */
8964 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008965 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008966 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8967 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8968 GUEST_INTR_STATE_NMI);
8969
8970 /*
8971 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8972 * here.., and there's no userspace involvement needed for PML.
8973 */
8974 return 1;
8975}
8976
Yunhong Jiang64672c92016-06-13 14:19:59 -07008977static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8978{
8979 kvm_lapic_expired_hv_timer(vcpu);
8980 return 1;
8981}
8982
Bandan Das41ab9372017-08-03 15:54:43 -04008983static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8984{
8985 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008986 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8987
8988 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008989 switch (address & VMX_EPTP_MT_MASK) {
8990 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008991 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008992 return false;
8993 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008994 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008995 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008996 return false;
8997 break;
8998 default:
8999 return false;
9000 }
9001
David Hildenbrandbb97a012017-08-10 23:15:28 +02009002 /* only 4 levels page-walk length are valid */
9003 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009004 return false;
9005
9006 /* Reserved bits should not be set */
9007 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9008 return false;
9009
9010 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009011 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009012 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009013 return false;
9014 }
9015
9016 return true;
9017}
9018
9019static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9020 struct vmcs12 *vmcs12)
9021{
9022 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9023 u64 address;
9024 bool accessed_dirty;
9025 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9026
9027 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9028 !nested_cpu_has_ept(vmcs12))
9029 return 1;
9030
9031 if (index >= VMFUNC_EPTP_ENTRIES)
9032 return 1;
9033
9034
9035 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9036 &address, index * 8, 8))
9037 return 1;
9038
David Hildenbrandbb97a012017-08-10 23:15:28 +02009039 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009040
9041 /*
9042 * If the (L2) guest does a vmfunc to the currently
9043 * active ept pointer, we don't have to do anything else
9044 */
9045 if (vmcs12->ept_pointer != address) {
9046 if (!valid_ept_address(vcpu, address))
9047 return 1;
9048
9049 kvm_mmu_unload(vcpu);
9050 mmu->ept_ad = accessed_dirty;
9051 mmu->base_role.ad_disabled = !accessed_dirty;
9052 vmcs12->ept_pointer = address;
9053 /*
9054 * TODO: Check what's the correct approach in case
9055 * mmu reload fails. Currently, we just let the next
9056 * reload potentially fail
9057 */
9058 kvm_mmu_reload(vcpu);
9059 }
9060
9061 return 0;
9062}
9063
Bandan Das2a499e42017-08-03 15:54:41 -04009064static int handle_vmfunc(struct kvm_vcpu *vcpu)
9065{
Bandan Das27c42a12017-08-03 15:54:42 -04009066 struct vcpu_vmx *vmx = to_vmx(vcpu);
9067 struct vmcs12 *vmcs12;
9068 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9069
9070 /*
9071 * VMFUNC is only supported for nested guests, but we always enable the
9072 * secondary control for simplicity; for non-nested mode, fake that we
9073 * didn't by injecting #UD.
9074 */
9075 if (!is_guest_mode(vcpu)) {
9076 kvm_queue_exception(vcpu, UD_VECTOR);
9077 return 1;
9078 }
9079
9080 vmcs12 = get_vmcs12(vcpu);
9081 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9082 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009083
9084 switch (function) {
9085 case 0:
9086 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9087 goto fail;
9088 break;
9089 default:
9090 goto fail;
9091 }
9092 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009093
9094fail:
9095 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9096 vmcs_read32(VM_EXIT_INTR_INFO),
9097 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009098 return 1;
9099}
9100
Nadav Har'El0140cae2011-05-25 23:06:28 +03009101/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009102 * The exit handlers return 1 if the exit was handled fully and guest execution
9103 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9104 * to be done to userspace and return 0.
9105 */
Mathias Krause772e0312012-08-30 01:30:19 +02009106static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009107 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9108 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009109 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009110 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009111 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009112 [EXIT_REASON_CR_ACCESS] = handle_cr,
9113 [EXIT_REASON_DR_ACCESS] = handle_dr,
9114 [EXIT_REASON_CPUID] = handle_cpuid,
9115 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9116 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9117 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9118 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009119 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009120 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009121 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009122 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009123 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009124 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009125 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009126 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009127 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009128 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009129 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009130 [EXIT_REASON_VMOFF] = handle_vmoff,
9131 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009132 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9133 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009134 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009135 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009136 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009137 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009138 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009139 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009140 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9141 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009142 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9143 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009144 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009145 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009146 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009147 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009148 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009149 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009150 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009151 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009152 [EXIT_REASON_XSAVES] = handle_xsaves,
9153 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009154 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009155 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009156 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009157 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009158};
9159
9160static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009161 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009162
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009163static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9164 struct vmcs12 *vmcs12)
9165{
9166 unsigned long exit_qualification;
9167 gpa_t bitmap, last_bitmap;
9168 unsigned int port;
9169 int size;
9170 u8 b;
9171
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009172 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009173 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009174
9175 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9176
9177 port = exit_qualification >> 16;
9178 size = (exit_qualification & 7) + 1;
9179
9180 last_bitmap = (gpa_t)-1;
9181 b = -1;
9182
9183 while (size > 0) {
9184 if (port < 0x8000)
9185 bitmap = vmcs12->io_bitmap_a;
9186 else if (port < 0x10000)
9187 bitmap = vmcs12->io_bitmap_b;
9188 else
Joe Perches1d804d02015-03-30 16:46:09 -07009189 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009190 bitmap += (port & 0x7fff) / 8;
9191
9192 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009193 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009194 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009195 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009196 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009197
9198 port++;
9199 size--;
9200 last_bitmap = bitmap;
9201 }
9202
Joe Perches1d804d02015-03-30 16:46:09 -07009203 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009204}
9205
Nadav Har'El644d7112011-05-25 23:12:35 +03009206/*
9207 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9208 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9209 * disinterest in the current event (read or write a specific MSR) by using an
9210 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9211 */
9212static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9213 struct vmcs12 *vmcs12, u32 exit_reason)
9214{
9215 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9216 gpa_t bitmap;
9217
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009218 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009219 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009220
9221 /*
9222 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9223 * for the four combinations of read/write and low/high MSR numbers.
9224 * First we need to figure out which of the four to use:
9225 */
9226 bitmap = vmcs12->msr_bitmap;
9227 if (exit_reason == EXIT_REASON_MSR_WRITE)
9228 bitmap += 2048;
9229 if (msr_index >= 0xc0000000) {
9230 msr_index -= 0xc0000000;
9231 bitmap += 1024;
9232 }
9233
9234 /* Then read the msr_index'th bit from this bitmap: */
9235 if (msr_index < 1024*8) {
9236 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009237 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009238 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009239 return 1 & (b >> (msr_index & 7));
9240 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009241 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009242}
9243
9244/*
9245 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9246 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9247 * intercept (via guest_host_mask etc.) the current event.
9248 */
9249static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9250 struct vmcs12 *vmcs12)
9251{
9252 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9253 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009254 int reg;
9255 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009256
9257 switch ((exit_qualification >> 4) & 3) {
9258 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009259 reg = (exit_qualification >> 8) & 15;
9260 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009261 switch (cr) {
9262 case 0:
9263 if (vmcs12->cr0_guest_host_mask &
9264 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009265 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009266 break;
9267 case 3:
9268 if ((vmcs12->cr3_target_count >= 1 &&
9269 vmcs12->cr3_target_value0 == val) ||
9270 (vmcs12->cr3_target_count >= 2 &&
9271 vmcs12->cr3_target_value1 == val) ||
9272 (vmcs12->cr3_target_count >= 3 &&
9273 vmcs12->cr3_target_value2 == val) ||
9274 (vmcs12->cr3_target_count >= 4 &&
9275 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009276 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009277 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009278 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009279 break;
9280 case 4:
9281 if (vmcs12->cr4_guest_host_mask &
9282 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009283 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009284 break;
9285 case 8:
9286 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009287 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009288 break;
9289 }
9290 break;
9291 case 2: /* clts */
9292 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9293 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009294 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009295 break;
9296 case 1: /* mov from cr */
9297 switch (cr) {
9298 case 3:
9299 if (vmcs12->cpu_based_vm_exec_control &
9300 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009301 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009302 break;
9303 case 8:
9304 if (vmcs12->cpu_based_vm_exec_control &
9305 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009306 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009307 break;
9308 }
9309 break;
9310 case 3: /* lmsw */
9311 /*
9312 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9313 * cr0. Other attempted changes are ignored, with no exit.
9314 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009315 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009316 if (vmcs12->cr0_guest_host_mask & 0xe &
9317 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009318 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009319 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9320 !(vmcs12->cr0_read_shadow & 0x1) &&
9321 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009322 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009323 break;
9324 }
Joe Perches1d804d02015-03-30 16:46:09 -07009325 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009326}
9327
Liran Alona7cde482018-06-23 02:35:10 +03009328static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9329 struct vmcs12 *vmcs12, gpa_t bitmap)
9330{
9331 u32 vmx_instruction_info;
9332 unsigned long field;
9333 u8 b;
9334
9335 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9336 return true;
9337
9338 /* Decode instruction info and find the field to access */
9339 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9340 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9341
9342 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9343 if (field >> 15)
9344 return true;
9345
9346 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9347 return true;
9348
9349 return 1 & (b >> (field & 7));
9350}
9351
Nadav Har'El644d7112011-05-25 23:12:35 +03009352/*
9353 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9354 * should handle it ourselves in L0 (and then continue L2). Only call this
9355 * when in is_guest_mode (L2).
9356 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009357static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009358{
Nadav Har'El644d7112011-05-25 23:12:35 +03009359 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9360 struct vcpu_vmx *vmx = to_vmx(vcpu);
9361 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9362
Jim Mattson4f350c62017-09-14 16:31:44 -07009363 if (vmx->nested.nested_run_pending)
9364 return false;
9365
9366 if (unlikely(vmx->fail)) {
9367 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9368 vmcs_read32(VM_INSTRUCTION_ERROR));
9369 return true;
9370 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009371
David Matlackc9f04402017-08-01 14:00:40 -07009372 /*
9373 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009374 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9375 * Page). The CPU may write to these pages via their host
9376 * physical address while L2 is running, bypassing any
9377 * address-translation-based dirty tracking (e.g. EPT write
9378 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009379 *
9380 * Mark them dirty on every exit from L2 to prevent them from
9381 * getting out of sync with dirty tracking.
9382 */
9383 nested_mark_vmcs12_pages_dirty(vcpu);
9384
Jim Mattson4f350c62017-09-14 16:31:44 -07009385 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9386 vmcs_readl(EXIT_QUALIFICATION),
9387 vmx->idt_vectoring_info,
9388 intr_info,
9389 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9390 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009391
9392 switch (exit_reason) {
9393 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009394 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009395 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009396 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009397 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009398 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009399 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009400 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009401 else if (is_debug(intr_info) &&
9402 vcpu->guest_debug &
9403 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9404 return false;
9405 else if (is_breakpoint(intr_info) &&
9406 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9407 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009408 return vmcs12->exception_bitmap &
9409 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9410 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009411 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009412 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009413 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009414 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009415 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009416 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009417 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009418 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009419 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009420 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009421 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009422 case EXIT_REASON_HLT:
9423 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9424 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009425 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009426 case EXIT_REASON_INVLPG:
9427 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9428 case EXIT_REASON_RDPMC:
9429 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009430 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009431 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009432 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009433 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009434 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009435 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009436 case EXIT_REASON_VMREAD:
9437 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9438 vmcs12->vmread_bitmap);
9439 case EXIT_REASON_VMWRITE:
9440 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9441 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009442 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9443 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009444 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009445 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009446 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009447 /*
9448 * VMX instructions trap unconditionally. This allows L1 to
9449 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9450 */
Joe Perches1d804d02015-03-30 16:46:09 -07009451 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009452 case EXIT_REASON_CR_ACCESS:
9453 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9454 case EXIT_REASON_DR_ACCESS:
9455 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9456 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009457 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009458 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9459 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009460 case EXIT_REASON_MSR_READ:
9461 case EXIT_REASON_MSR_WRITE:
9462 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9463 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009464 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009465 case EXIT_REASON_MWAIT_INSTRUCTION:
9466 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009467 case EXIT_REASON_MONITOR_TRAP_FLAG:
9468 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009469 case EXIT_REASON_MONITOR_INSTRUCTION:
9470 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9471 case EXIT_REASON_PAUSE_INSTRUCTION:
9472 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9473 nested_cpu_has2(vmcs12,
9474 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9475 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009476 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009477 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009478 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009479 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009480 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009481 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009482 /*
9483 * The controls for "virtualize APIC accesses," "APIC-
9484 * register virtualization," and "virtual-interrupt
9485 * delivery" only come from vmcs12.
9486 */
Joe Perches1d804d02015-03-30 16:46:09 -07009487 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009488 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009489 /*
9490 * L0 always deals with the EPT violation. If nested EPT is
9491 * used, and the nested mmu code discovers that the address is
9492 * missing in the guest EPT table (EPT12), the EPT violation
9493 * will be injected with nested_ept_inject_page_fault()
9494 */
Joe Perches1d804d02015-03-30 16:46:09 -07009495 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009496 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009497 /*
9498 * L2 never uses directly L1's EPT, but rather L0's own EPT
9499 * table (shadow on EPT) or a merged EPT table that L0 built
9500 * (EPT on EPT). So any problems with the structure of the
9501 * table is L0's fault.
9502 */
Joe Perches1d804d02015-03-30 16:46:09 -07009503 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009504 case EXIT_REASON_INVPCID:
9505 return
9506 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9507 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009508 case EXIT_REASON_WBINVD:
9509 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9510 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009511 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009512 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9513 /*
9514 * This should never happen, since it is not possible to
9515 * set XSS to a non-zero value---neither in L1 nor in L2.
9516 * If if it were, XSS would have to be checked against
9517 * the XSS exit bitmap in vmcs12.
9518 */
9519 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009520 case EXIT_REASON_PREEMPTION_TIMER:
9521 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009522 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009523 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009524 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009525 case EXIT_REASON_VMFUNC:
9526 /* VM functions are emulated through L2->L0 vmexits. */
9527 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009528 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009529 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009530 }
9531}
9532
Paolo Bonzini7313c692017-07-27 10:31:25 +02009533static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9534{
9535 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9536
9537 /*
9538 * At this point, the exit interruption info in exit_intr_info
9539 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9540 * we need to query the in-kernel LAPIC.
9541 */
9542 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9543 if ((exit_intr_info &
9544 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9545 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9546 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9547 vmcs12->vm_exit_intr_error_code =
9548 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9549 }
9550
9551 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9552 vmcs_readl(EXIT_QUALIFICATION));
9553 return 1;
9554}
9555
Avi Kivity586f9602010-11-18 13:09:54 +02009556static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9557{
9558 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9559 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9560}
9561
Kai Huanga3eaa862015-11-04 13:46:05 +08009562static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009563{
Kai Huanga3eaa862015-11-04 13:46:05 +08009564 if (vmx->pml_pg) {
9565 __free_page(vmx->pml_pg);
9566 vmx->pml_pg = NULL;
9567 }
Kai Huang843e4332015-01-28 10:54:28 +08009568}
9569
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009570static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009571{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009572 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009573 u64 *pml_buf;
9574 u16 pml_idx;
9575
9576 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9577
9578 /* Do nothing if PML buffer is empty */
9579 if (pml_idx == (PML_ENTITY_NUM - 1))
9580 return;
9581
9582 /* PML index always points to next available PML buffer entity */
9583 if (pml_idx >= PML_ENTITY_NUM)
9584 pml_idx = 0;
9585 else
9586 pml_idx++;
9587
9588 pml_buf = page_address(vmx->pml_pg);
9589 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9590 u64 gpa;
9591
9592 gpa = pml_buf[pml_idx];
9593 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009594 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009595 }
9596
9597 /* reset PML index */
9598 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9599}
9600
9601/*
9602 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9603 * Called before reporting dirty_bitmap to userspace.
9604 */
9605static void kvm_flush_pml_buffers(struct kvm *kvm)
9606{
9607 int i;
9608 struct kvm_vcpu *vcpu;
9609 /*
9610 * We only need to kick vcpu out of guest mode here, as PML buffer
9611 * is flushed at beginning of all VMEXITs, and it's obvious that only
9612 * vcpus running in guest are possible to have unflushed GPAs in PML
9613 * buffer.
9614 */
9615 kvm_for_each_vcpu(i, vcpu, kvm)
9616 kvm_vcpu_kick(vcpu);
9617}
9618
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009619static void vmx_dump_sel(char *name, uint32_t sel)
9620{
9621 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009622 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009623 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9624 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9625 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9626}
9627
9628static void vmx_dump_dtsel(char *name, uint32_t limit)
9629{
9630 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9631 name, vmcs_read32(limit),
9632 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9633}
9634
9635static void dump_vmcs(void)
9636{
9637 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9638 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9639 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9640 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9641 u32 secondary_exec_control = 0;
9642 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009643 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009644 int i, n;
9645
9646 if (cpu_has_secondary_exec_ctrls())
9647 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9648
9649 pr_err("*** Guest State ***\n");
9650 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9651 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9652 vmcs_readl(CR0_GUEST_HOST_MASK));
9653 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9654 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9655 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9656 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9657 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9658 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009659 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9660 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9661 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9662 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009663 }
9664 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9665 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9666 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9667 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9668 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9669 vmcs_readl(GUEST_SYSENTER_ESP),
9670 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9671 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9672 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9673 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9674 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9675 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9676 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9677 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9678 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9679 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9680 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9681 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9682 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009683 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9684 efer, vmcs_read64(GUEST_IA32_PAT));
9685 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9686 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009687 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009688 if (cpu_has_load_perf_global_ctrl &&
9689 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009690 pr_err("PerfGlobCtl = 0x%016llx\n",
9691 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009692 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009693 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009694 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9695 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9696 vmcs_read32(GUEST_ACTIVITY_STATE));
9697 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9698 pr_err("InterruptStatus = %04x\n",
9699 vmcs_read16(GUEST_INTR_STATUS));
9700
9701 pr_err("*** Host State ***\n");
9702 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9703 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9704 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9705 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9706 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9707 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9708 vmcs_read16(HOST_TR_SELECTOR));
9709 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9710 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9711 vmcs_readl(HOST_TR_BASE));
9712 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9713 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9714 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9715 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9716 vmcs_readl(HOST_CR4));
9717 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9718 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9719 vmcs_read32(HOST_IA32_SYSENTER_CS),
9720 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9721 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009722 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9723 vmcs_read64(HOST_IA32_EFER),
9724 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009725 if (cpu_has_load_perf_global_ctrl &&
9726 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009727 pr_err("PerfGlobCtl = 0x%016llx\n",
9728 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009729
9730 pr_err("*** Control State ***\n");
9731 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9732 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9733 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9734 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9735 vmcs_read32(EXCEPTION_BITMAP),
9736 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9737 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9738 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9739 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9740 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9741 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9742 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9743 vmcs_read32(VM_EXIT_INTR_INFO),
9744 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9745 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9746 pr_err(" reason=%08x qualification=%016lx\n",
9747 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9748 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9749 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9750 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009751 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009752 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009753 pr_err("TSC Multiplier = 0x%016llx\n",
9754 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009755 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9756 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9757 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9758 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9759 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009760 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009761 n = vmcs_read32(CR3_TARGET_COUNT);
9762 for (i = 0; i + 1 < n; i += 4)
9763 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9764 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9765 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9766 if (i < n)
9767 pr_err("CR3 target%u=%016lx\n",
9768 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9769 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9770 pr_err("PLE Gap=%08x Window=%08x\n",
9771 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9772 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9773 pr_err("Virtual processor ID = 0x%04x\n",
9774 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9775}
9776
Avi Kivity6aa8b732006-12-10 02:21:36 -08009777/*
9778 * The guest has exited. See if we can fix it or if we need userspace
9779 * assistance.
9780 */
Avi Kivity851ba692009-08-24 11:10:17 +03009781static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009782{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009783 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009784 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009785 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009786
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009787 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9788
Kai Huang843e4332015-01-28 10:54:28 +08009789 /*
9790 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9791 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9792 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9793 * mode as if vcpus is in root mode, the PML buffer must has been
9794 * flushed already.
9795 */
9796 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009797 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009798
Mohammed Gamal80ced182009-09-01 12:48:18 +02009799 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009800 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009801 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009802
Paolo Bonzini7313c692017-07-27 10:31:25 +02009803 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9804 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009805
Mohammed Gamal51207022010-05-31 22:40:54 +03009806 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009807 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009808 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9809 vcpu->run->fail_entry.hardware_entry_failure_reason
9810 = exit_reason;
9811 return 0;
9812 }
9813
Avi Kivity29bd8a72007-09-10 17:27:03 +03009814 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009815 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9816 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009817 = vmcs_read32(VM_INSTRUCTION_ERROR);
9818 return 0;
9819 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009820
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009821 /*
9822 * Note:
9823 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9824 * delivery event since it indicates guest is accessing MMIO.
9825 * The vm-exit can be triggered again after return to guest that
9826 * will cause infinite loop.
9827 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009828 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009829 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009830 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009831 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009832 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9833 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9834 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009835 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009836 vcpu->run->internal.data[0] = vectoring_info;
9837 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009838 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9839 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9840 vcpu->run->internal.ndata++;
9841 vcpu->run->internal.data[3] =
9842 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9843 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009844 return 0;
9845 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009846
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009847 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009848 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9849 if (vmx_interrupt_allowed(vcpu)) {
9850 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9851 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9852 vcpu->arch.nmi_pending) {
9853 /*
9854 * This CPU don't support us in finding the end of an
9855 * NMI-blocked window if the guest runs with IRQs
9856 * disabled. So we pull the trigger after 1 s of
9857 * futile waiting, but inform the user about this.
9858 */
9859 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9860 "state on VCPU %d after 1 s timeout\n",
9861 __func__, vcpu->vcpu_id);
9862 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9863 }
9864 }
9865
Avi Kivity6aa8b732006-12-10 02:21:36 -08009866 if (exit_reason < kvm_vmx_max_exit_handlers
9867 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009868 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009869 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009870 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9871 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009872 kvm_queue_exception(vcpu, UD_VECTOR);
9873 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009874 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009875}
9876
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009877static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009878{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009879 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9880
9881 if (is_guest_mode(vcpu) &&
9882 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9883 return;
9884
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009885 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009886 vmcs_write32(TPR_THRESHOLD, 0);
9887 return;
9888 }
9889
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009890 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009891}
9892
Jim Mattson8d860bb2018-05-09 16:56:05 -04009893static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009894{
9895 u32 sec_exec_control;
9896
Jim Mattson8d860bb2018-05-09 16:56:05 -04009897 if (!lapic_in_kernel(vcpu))
9898 return;
9899
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009900 /* Postpone execution until vmcs01 is the current VMCS. */
9901 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009902 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009903 return;
9904 }
9905
Paolo Bonzini35754c92015-07-29 12:05:37 +02009906 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009907 return;
9908
9909 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009910 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9911 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009912
Jim Mattson8d860bb2018-05-09 16:56:05 -04009913 switch (kvm_get_apic_mode(vcpu)) {
9914 case LAPIC_MODE_INVALID:
9915 WARN_ONCE(true, "Invalid local APIC state");
9916 case LAPIC_MODE_DISABLED:
9917 break;
9918 case LAPIC_MODE_XAPIC:
9919 if (flexpriority_enabled) {
9920 sec_exec_control |=
9921 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9922 vmx_flush_tlb(vcpu, true);
9923 }
9924 break;
9925 case LAPIC_MODE_X2APIC:
9926 if (cpu_has_vmx_virtualize_x2apic_mode())
9927 sec_exec_control |=
9928 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9929 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009930 }
9931 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9932
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009933 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009934}
9935
Tang Chen38b99172014-09-24 15:57:54 +08009936static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9937{
Jim Mattsonab5df312018-05-09 17:02:03 -04009938 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009939 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009940 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009941 }
Tang Chen38b99172014-09-24 15:57:54 +08009942}
9943
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009944static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009945{
9946 u16 status;
9947 u8 old;
9948
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009949 if (max_isr == -1)
9950 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009951
9952 status = vmcs_read16(GUEST_INTR_STATUS);
9953 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009954 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009955 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009956 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009957 vmcs_write16(GUEST_INTR_STATUS, status);
9958 }
9959}
9960
9961static void vmx_set_rvi(int vector)
9962{
9963 u16 status;
9964 u8 old;
9965
Wei Wang4114c272014-11-05 10:53:43 +08009966 if (vector == -1)
9967 vector = 0;
9968
Yang Zhangc7c9c562013-01-25 10:18:51 +08009969 status = vmcs_read16(GUEST_INTR_STATUS);
9970 old = (u8)status & 0xff;
9971 if ((u8)vector != old) {
9972 status &= ~0xff;
9973 status |= (u8)vector;
9974 vmcs_write16(GUEST_INTR_STATUS, status);
9975 }
9976}
9977
9978static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9979{
Liran Alon851c1a182017-12-24 18:12:56 +02009980 /*
9981 * When running L2, updating RVI is only relevant when
9982 * vmcs12 virtual-interrupt-delivery enabled.
9983 * However, it can be enabled only when L1 also
9984 * intercepts external-interrupts and in that case
9985 * we should not update vmcs02 RVI but instead intercept
9986 * interrupt. Therefore, do nothing when running L2.
9987 */
9988 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009989 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009990}
9991
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009992static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009993{
9994 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009995 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009996 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009997
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009998 WARN_ON(!vcpu->arch.apicv_active);
9999 if (pi_test_on(&vmx->pi_desc)) {
10000 pi_clear_on(&vmx->pi_desc);
10001 /*
10002 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10003 * But on x86 this is just a compiler barrier anyway.
10004 */
10005 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010006 max_irr_updated =
10007 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10008
10009 /*
10010 * If we are running L2 and L1 has a new pending interrupt
10011 * which can be injected, we should re-evaluate
10012 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010013 * If L1 intercepts external-interrupts, we should
10014 * exit from L2 to L1. Otherwise, interrupt should be
10015 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010016 */
Liran Alon851c1a182017-12-24 18:12:56 +020010017 if (is_guest_mode(vcpu) && max_irr_updated) {
10018 if (nested_exit_on_intr(vcpu))
10019 kvm_vcpu_exiting_guest_mode(vcpu);
10020 else
10021 kvm_make_request(KVM_REQ_EVENT, vcpu);
10022 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010023 } else {
10024 max_irr = kvm_lapic_find_highest_irr(vcpu);
10025 }
10026 vmx_hwapic_irr_update(vcpu, max_irr);
10027 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010028}
10029
Andrey Smetanin63086302015-11-10 15:36:32 +030010030static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010031{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010032 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010033 return;
10034
Yang Zhangc7c9c562013-01-25 10:18:51 +080010035 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10036 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10037 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10038 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10039}
10040
Paolo Bonzini967235d2016-12-19 14:03:45 +010010041static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10042{
10043 struct vcpu_vmx *vmx = to_vmx(vcpu);
10044
10045 pi_clear_on(&vmx->pi_desc);
10046 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10047}
10048
Avi Kivity51aa01d2010-07-20 14:31:20 +030010049static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010050{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010051 u32 exit_intr_info = 0;
10052 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010053
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010054 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10055 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010056 return;
10057
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010058 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10059 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10060 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010061
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010062 /* if exit due to PF check for async PF */
10063 if (is_page_fault(exit_intr_info))
10064 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10065
Andi Kleena0861c02009-06-08 17:37:09 +080010066 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010067 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10068 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010069 kvm_machine_check();
10070
Gleb Natapov20f65982009-05-11 13:35:55 +030010071 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010072 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010073 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010074 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010075 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010076 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010077}
Gleb Natapov20f65982009-05-11 13:35:55 +030010078
Yang Zhanga547c6d2013-04-11 19:25:10 +080010079static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10080{
10081 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10082
Yang Zhanga547c6d2013-04-11 19:25:10 +080010083 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10084 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10085 unsigned int vector;
10086 unsigned long entry;
10087 gate_desc *desc;
10088 struct vcpu_vmx *vmx = to_vmx(vcpu);
10089#ifdef CONFIG_X86_64
10090 unsigned long tmp;
10091#endif
10092
10093 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10094 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010095 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010096 asm volatile(
10097#ifdef CONFIG_X86_64
10098 "mov %%" _ASM_SP ", %[sp]\n\t"
10099 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10100 "push $%c[ss]\n\t"
10101 "push %[sp]\n\t"
10102#endif
10103 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010104 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010105 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010106 :
10107#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010108 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010109#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010110 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010111 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010112 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010113 [ss]"i"(__KERNEL_DS),
10114 [cs]"i"(__KERNEL_CS)
10115 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010116 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010117}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010118STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010119
Tom Lendackybc226f02018-05-10 22:06:39 +020010120static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010121{
Tom Lendackybc226f02018-05-10 22:06:39 +020010122 switch (index) {
10123 case MSR_IA32_SMBASE:
10124 /*
10125 * We cannot do SMM unless we can run the guest in big
10126 * real mode.
10127 */
10128 return enable_unrestricted_guest || emulate_invalid_guest_state;
10129 case MSR_AMD64_VIRT_SPEC_CTRL:
10130 /* This is AMD only. */
10131 return false;
10132 default:
10133 return true;
10134 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010135}
10136
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010137static bool vmx_mpx_supported(void)
10138{
10139 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10140 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10141}
10142
Wanpeng Li55412b22014-12-02 19:21:30 +080010143static bool vmx_xsaves_supported(void)
10144{
10145 return vmcs_config.cpu_based_2nd_exec_ctrl &
10146 SECONDARY_EXEC_XSAVES;
10147}
10148
Avi Kivity51aa01d2010-07-20 14:31:20 +030010149static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10150{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010151 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010152 bool unblock_nmi;
10153 u8 vector;
10154 bool idtv_info_valid;
10155
10156 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010157
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010158 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010159 if (vmx->loaded_vmcs->nmi_known_unmasked)
10160 return;
10161 /*
10162 * Can't use vmx->exit_intr_info since we're not sure what
10163 * the exit reason is.
10164 */
10165 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10166 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10167 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10168 /*
10169 * SDM 3: 27.7.1.2 (September 2008)
10170 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10171 * a guest IRET fault.
10172 * SDM 3: 23.2.2 (September 2008)
10173 * Bit 12 is undefined in any of the following cases:
10174 * If the VM exit sets the valid bit in the IDT-vectoring
10175 * information field.
10176 * If the VM exit is due to a double fault.
10177 */
10178 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10179 vector != DF_VECTOR && !idtv_info_valid)
10180 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10181 GUEST_INTR_STATE_NMI);
10182 else
10183 vmx->loaded_vmcs->nmi_known_unmasked =
10184 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10185 & GUEST_INTR_STATE_NMI);
10186 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10187 vmx->loaded_vmcs->vnmi_blocked_time +=
10188 ktime_to_ns(ktime_sub(ktime_get(),
10189 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010190}
10191
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010192static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010193 u32 idt_vectoring_info,
10194 int instr_len_field,
10195 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010196{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010197 u8 vector;
10198 int type;
10199 bool idtv_info_valid;
10200
10201 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010202
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010203 vcpu->arch.nmi_injected = false;
10204 kvm_clear_exception_queue(vcpu);
10205 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010206
10207 if (!idtv_info_valid)
10208 return;
10209
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010210 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010211
Avi Kivity668f6122008-07-02 09:28:55 +030010212 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10213 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010214
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010215 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010216 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010217 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010218 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010219 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010220 * Clear bit "block by NMI" before VM entry if a NMI
10221 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010222 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010223 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010224 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010225 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010226 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010227 /* fall through */
10228 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010229 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010230 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010231 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010232 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010233 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010234 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010235 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010236 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010237 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010238 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010239 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010240 break;
10241 default:
10242 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010243 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010244}
10245
Avi Kivity83422e12010-07-20 14:43:23 +030010246static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10247{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010248 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010249 VM_EXIT_INSTRUCTION_LEN,
10250 IDT_VECTORING_ERROR_CODE);
10251}
10252
Avi Kivityb463a6f2010-07-20 15:06:17 +030010253static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10254{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010255 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010256 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10257 VM_ENTRY_INSTRUCTION_LEN,
10258 VM_ENTRY_EXCEPTION_ERROR_CODE);
10259
10260 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10261}
10262
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010263static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10264{
10265 int i, nr_msrs;
10266 struct perf_guest_switch_msr *msrs;
10267
10268 msrs = perf_guest_get_msrs(&nr_msrs);
10269
10270 if (!msrs)
10271 return;
10272
10273 for (i = 0; i < nr_msrs; i++)
10274 if (msrs[i].host == msrs[i].guest)
10275 clear_atomic_switch_msr(vmx, msrs[i].msr);
10276 else
10277 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10278 msrs[i].host);
10279}
10280
Jiang Biao33365e72016-11-03 15:03:37 +080010281static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010282{
10283 struct vcpu_vmx *vmx = to_vmx(vcpu);
10284 u64 tscl;
10285 u32 delta_tsc;
10286
10287 if (vmx->hv_deadline_tsc == -1)
10288 return;
10289
10290 tscl = rdtsc();
10291 if (vmx->hv_deadline_tsc > tscl)
10292 /* sure to be 32 bit only because checked on set_hv_timer */
10293 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10294 cpu_preemption_timer_multi);
10295 else
10296 delta_tsc = 0;
10297
10298 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10299}
10300
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010301static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010302{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010303 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010304 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010305
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010306 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010307 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010308 vmx->loaded_vmcs->soft_vnmi_blocked))
10309 vmx->loaded_vmcs->entry_time = ktime_get();
10310
Avi Kivity104f2262010-11-18 13:12:52 +020010311 /* Don't enter VMX if guest state is invalid, let the exit handler
10312 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010313 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010314 return;
10315
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010316 if (vmx->ple_window_dirty) {
10317 vmx->ple_window_dirty = false;
10318 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10319 }
10320
Abel Gordon012f83c2013-04-18 14:39:25 +030010321 if (vmx->nested.sync_shadow_vmcs) {
10322 copy_vmcs12_to_shadow(vmx);
10323 vmx->nested.sync_shadow_vmcs = false;
10324 }
10325
Avi Kivity104f2262010-11-18 13:12:52 +020010326 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10327 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10328 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10329 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10330
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010331 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010332 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010333 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010334 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010335 }
10336
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010337 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010338 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010339 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010340 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010341 }
10342
Avi Kivity104f2262010-11-18 13:12:52 +020010343 /* When single-stepping over STI and MOV SS, we must clear the
10344 * corresponding interruptibility bits in the guest state. Otherwise
10345 * vmentry fails as it then expects bit 14 (BS) in pending debug
10346 * exceptions being set, but that's not correct for the guest debugging
10347 * case. */
10348 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10349 vmx_set_interrupt_shadow(vcpu, 0);
10350
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010351 if (static_cpu_has(X86_FEATURE_PKU) &&
10352 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10353 vcpu->arch.pkru != vmx->host_pkru)
10354 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010355
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010356 atomic_switch_perf_msrs(vmx);
10357
Yunhong Jiang64672c92016-06-13 14:19:59 -070010358 vmx_arm_hv_timer(vcpu);
10359
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010360 /*
10361 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10362 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10363 * is no need to worry about the conditional branch over the wrmsr
10364 * being speculatively taken.
10365 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010366 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010367
Nadav Har'Eld462b812011-05-24 15:26:10 +030010368 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010369
10370 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10371 (unsigned long)&current_evmcs->host_rsp : 0;
10372
Avi Kivity104f2262010-11-18 13:12:52 +020010373 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010374 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010375 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10376 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10377 "push %%" _ASM_CX " \n\t"
10378 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010379 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010380 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010381 /* Avoid VMWRITE when Enlightened VMCS is in use */
10382 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10383 "jz 2f \n\t"
10384 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10385 "jmp 1f \n\t"
10386 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010387 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010388 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010389 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010390 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10391 "mov %%cr2, %%" _ASM_DX " \n\t"
10392 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010393 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010394 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010395 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010396 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010397 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010398 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010399 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10400 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10401 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10402 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10403 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10404 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010405#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010406 "mov %c[r8](%0), %%r8 \n\t"
10407 "mov %c[r9](%0), %%r9 \n\t"
10408 "mov %c[r10](%0), %%r10 \n\t"
10409 "mov %c[r11](%0), %%r11 \n\t"
10410 "mov %c[r12](%0), %%r12 \n\t"
10411 "mov %c[r13](%0), %%r13 \n\t"
10412 "mov %c[r14](%0), %%r14 \n\t"
10413 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010414#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010415 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010416
Avi Kivity6aa8b732006-12-10 02:21:36 -080010417 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010418 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010419 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010420 "jmp 2f \n\t"
10421 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10422 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010423 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010424 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010425 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010426 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010427 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10428 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10429 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10430 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10431 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10432 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10433 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010434#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010435 "mov %%r8, %c[r8](%0) \n\t"
10436 "mov %%r9, %c[r9](%0) \n\t"
10437 "mov %%r10, %c[r10](%0) \n\t"
10438 "mov %%r11, %c[r11](%0) \n\t"
10439 "mov %%r12, %c[r12](%0) \n\t"
10440 "mov %%r13, %c[r13](%0) \n\t"
10441 "mov %%r14, %c[r14](%0) \n\t"
10442 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010443 "xor %%r8d, %%r8d \n\t"
10444 "xor %%r9d, %%r9d \n\t"
10445 "xor %%r10d, %%r10d \n\t"
10446 "xor %%r11d, %%r11d \n\t"
10447 "xor %%r12d, %%r12d \n\t"
10448 "xor %%r13d, %%r13d \n\t"
10449 "xor %%r14d, %%r14d \n\t"
10450 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010451#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010452 "mov %%cr2, %%" _ASM_AX " \n\t"
10453 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010454
Jim Mattson0cb5b302018-01-03 14:31:38 -080010455 "xor %%eax, %%eax \n\t"
10456 "xor %%ebx, %%ebx \n\t"
10457 "xor %%esi, %%esi \n\t"
10458 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010459 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010460 ".pushsection .rodata \n\t"
10461 ".global vmx_return \n\t"
10462 "vmx_return: " _ASM_PTR " 2b \n\t"
10463 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010464 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010465 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010466 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010467 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010468 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10469 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10470 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10471 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10472 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10473 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10474 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010475#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010476 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10477 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10478 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10479 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10480 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10481 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10482 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10483 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010484#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010485 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10486 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010487 : "cc", "memory"
10488#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010489 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010490 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010491#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010492 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010493#endif
10494 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010495
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010496 /*
10497 * We do not use IBRS in the kernel. If this vCPU has used the
10498 * SPEC_CTRL MSR it may have left it on; save the value and
10499 * turn it off. This is much more efficient than blindly adding
10500 * it to the atomic save/restore list. Especially as the former
10501 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10502 *
10503 * For non-nested case:
10504 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10505 * save it.
10506 *
10507 * For nested case:
10508 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10509 * save it.
10510 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010511 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010512 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010513
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010514 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010515
David Woodhouse117cc7a2018-01-12 11:11:27 +000010516 /* Eliminate branch target predictions from guest mode */
10517 vmexit_fill_RSB();
10518
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010519 /* All fields are clean at this point */
10520 if (static_branch_unlikely(&enable_evmcs))
10521 current_evmcs->hv_clean_fields |=
10522 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10523
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010524 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010525 if (vmx->host_debugctlmsr)
10526 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010527
Avi Kivityaa67f602012-08-01 16:48:03 +030010528#ifndef CONFIG_X86_64
10529 /*
10530 * The sysexit path does not restore ds/es, so we must set them to
10531 * a reasonable value ourselves.
10532 *
10533 * We can't defer this to vmx_load_host_state() since that function
10534 * may be executed in interrupt context, which saves and restore segments
10535 * around it, nullifying its effect.
10536 */
10537 loadsegment(ds, __USER_DS);
10538 loadsegment(es, __USER_DS);
10539#endif
10540
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010541 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010542 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010543 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010544 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010545 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010546 vcpu->arch.regs_dirty = 0;
10547
Gleb Natapove0b890d2013-09-25 12:51:33 +030010548 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010549 * eager fpu is enabled if PKEY is supported and CR4 is switched
10550 * back on host, so it is safe to read guest PKRU from current
10551 * XSAVE.
10552 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010553 if (static_cpu_has(X86_FEATURE_PKU) &&
10554 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10555 vcpu->arch.pkru = __read_pkru();
10556 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010557 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010558 }
10559
Gleb Natapove0b890d2013-09-25 12:51:33 +030010560 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010561 vmx->idt_vectoring_info = 0;
10562
10563 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10564 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10565 return;
10566
10567 vmx->loaded_vmcs->launched = 1;
10568 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010569
Avi Kivity51aa01d2010-07-20 14:31:20 +030010570 vmx_complete_atomic_exit(vmx);
10571 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010572 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010573}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010574STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010575
Sean Christopherson434a1e92018-03-20 12:17:18 -070010576static struct kvm *vmx_vm_alloc(void)
10577{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010578 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010579 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010580}
10581
10582static void vmx_vm_free(struct kvm *kvm)
10583{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010584 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010585}
10586
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010587static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010588{
10589 struct vcpu_vmx *vmx = to_vmx(vcpu);
10590 int cpu;
10591
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010592 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010593 return;
10594
10595 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010596 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010597 vmx_vcpu_put(vcpu);
10598 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010599 put_cpu();
10600}
10601
Jim Mattson2f1fe812016-07-08 15:36:06 -070010602/*
10603 * Ensure that the current vmcs of the logical processor is the
10604 * vmcs01 of the vcpu before calling free_nested().
10605 */
10606static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10607{
10608 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010609
Christoffer Dallec7660c2017-12-04 21:35:23 +010010610 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010611 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010612 free_nested(vmx);
10613 vcpu_put(vcpu);
10614}
10615
Avi Kivity6aa8b732006-12-10 02:21:36 -080010616static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10617{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010618 struct vcpu_vmx *vmx = to_vmx(vcpu);
10619
Kai Huang843e4332015-01-28 10:54:28 +080010620 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010621 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010622 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010623 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010624 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010625 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010626 kfree(vmx->guest_msrs);
10627 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010628 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010629}
10630
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010631static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010632{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010633 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010634 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010635 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010636 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010637
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010638 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010639 return ERR_PTR(-ENOMEM);
10640
Wanpeng Li991e7a02015-09-16 17:30:05 +080010641 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010642
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010643 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10644 if (err)
10645 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010646
Peter Feiner4e595162016-07-07 14:49:58 -070010647 err = -ENOMEM;
10648
10649 /*
10650 * If PML is turned on, failure on enabling PML just results in failure
10651 * of creating the vcpu, therefore we can simplify PML logic (by
10652 * avoiding dealing with cases, such as enabling PML partially on vcpus
10653 * for the guest, etc.
10654 */
10655 if (enable_pml) {
10656 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10657 if (!vmx->pml_pg)
10658 goto uninit_vcpu;
10659 }
10660
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010661 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010662 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10663 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010664
Peter Feiner4e595162016-07-07 14:49:58 -070010665 if (!vmx->guest_msrs)
10666 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010667
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010668 err = alloc_loaded_vmcs(&vmx->vmcs01);
10669 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010670 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010671
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010672 msr_bitmap = vmx->vmcs01.msr_bitmap;
10673 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10674 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10675 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10676 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10677 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10678 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10679 vmx->msr_bitmap_mode = 0;
10680
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010681 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010682 cpu = get_cpu();
10683 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010684 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010685 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010686 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010687 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010688 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010689 err = alloc_apic_access_page(kvm);
10690 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010691 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010692 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010693
Sean Christophersone90008d2018-03-05 12:04:37 -080010694 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010695 err = init_rmode_identity_map(kvm);
10696 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010697 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010698 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010699
Wanpeng Li5c614b32015-10-13 09:18:36 -070010700 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010701 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10702 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010703 vmx->nested.vpid02 = allocate_vpid();
10704 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010705
Wincy Van705699a2015-02-03 23:58:17 +080010706 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010707 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010708
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010709 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10710
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010711 /*
10712 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10713 * or POSTED_INTR_WAKEUP_VECTOR.
10714 */
10715 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10716 vmx->pi_desc.sn = 1;
10717
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010718 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010719
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010720free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010721 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010722 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010723free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010724 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010725free_pml:
10726 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010727uninit_vcpu:
10728 kvm_vcpu_uninit(&vmx->vcpu);
10729free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010730 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010731 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010732 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010733}
10734
Wanpeng Lib31c1142018-03-12 04:53:04 -070010735static int vmx_vm_init(struct kvm *kvm)
10736{
Tianyu Lan877ad952018-07-19 08:40:23 +000010737 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
10738
Wanpeng Lib31c1142018-03-12 04:53:04 -070010739 if (!ple_gap)
10740 kvm->arch.pause_in_guest = true;
10741 return 0;
10742}
10743
Yang, Sheng002c7f72007-07-31 14:23:01 +030010744static void __init vmx_check_processor_compat(void *rtn)
10745{
10746 struct vmcs_config vmcs_conf;
10747
10748 *(int *)rtn = 0;
10749 if (setup_vmcs_config(&vmcs_conf) < 0)
10750 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010751 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010752 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10753 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10754 smp_processor_id());
10755 *(int *)rtn = -EIO;
10756 }
10757}
10758
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010759static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010760{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010761 u8 cache;
10762 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010763
Sheng Yang522c68c2009-04-27 20:35:43 +080010764 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010765 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010766 * 2. EPT with VT-d:
10767 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010768 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010769 * b. VT-d with snooping control feature: snooping control feature of
10770 * VT-d engine can guarantee the cache correctness. Just set it
10771 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010772 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010773 * consistent with host MTRR
10774 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010775 if (is_mmio) {
10776 cache = MTRR_TYPE_UNCACHABLE;
10777 goto exit;
10778 }
10779
10780 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010781 ipat = VMX_EPT_IPAT_BIT;
10782 cache = MTRR_TYPE_WRBACK;
10783 goto exit;
10784 }
10785
10786 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10787 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010788 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010789 cache = MTRR_TYPE_WRBACK;
10790 else
10791 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010792 goto exit;
10793 }
10794
Xiao Guangrongff536042015-06-15 16:55:22 +080010795 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010796
10797exit:
10798 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010799}
10800
Sheng Yang17cc3932010-01-05 19:02:27 +080010801static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010802{
Sheng Yang878403b2010-01-05 19:02:29 +080010803 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10804 return PT_DIRECTORY_LEVEL;
10805 else
10806 /* For shadow and EPT supported 1GB page */
10807 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010808}
10809
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010810static void vmcs_set_secondary_exec_control(u32 new_ctl)
10811{
10812 /*
10813 * These bits in the secondary execution controls field
10814 * are dynamic, the others are mostly based on the hypervisor
10815 * architecture and the guest's CPUID. Do not touch the
10816 * dynamic bits.
10817 */
10818 u32 mask =
10819 SECONDARY_EXEC_SHADOW_VMCS |
10820 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010821 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10822 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010823
10824 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10825
10826 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10827 (new_ctl & ~mask) | (cur_ctl & mask));
10828}
10829
David Matlack8322ebb2016-11-29 18:14:09 -080010830/*
10831 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10832 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10833 */
10834static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10835{
10836 struct vcpu_vmx *vmx = to_vmx(vcpu);
10837 struct kvm_cpuid_entry2 *entry;
10838
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010839 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10840 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010841
10842#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10843 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010844 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010845} while (0)
10846
10847 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10848 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10849 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10850 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10851 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10852 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10853 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10854 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10855 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10856 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10857 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10858 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10859 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10860 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10861 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10862
10863 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10864 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10865 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10866 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10867 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010868 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010869
10870#undef cr4_fixed1_update
10871}
10872
Sheng Yang0e851882009-12-18 16:48:46 +080010873static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10874{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010875 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010876
Paolo Bonzini80154d72017-08-24 13:55:35 +020010877 if (cpu_has_secondary_exec_ctrls()) {
10878 vmx_compute_secondary_exec_control(vmx);
10879 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010880 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010881
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010882 if (nested_vmx_allowed(vcpu))
10883 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10884 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10885 else
10886 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10887 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010888
10889 if (nested_vmx_allowed(vcpu))
10890 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010891}
10892
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010893static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10894{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010895 if (func == 1 && nested)
10896 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010897}
10898
Yang Zhang25d92082013-08-06 12:00:32 +030010899static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10900 struct x86_exception *fault)
10901{
Jan Kiszka533558b2014-01-04 18:47:20 +010010902 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010903 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010904 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010905 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010906
Bandan Dasc5f983f2017-05-05 15:25:14 -040010907 if (vmx->nested.pml_full) {
10908 exit_reason = EXIT_REASON_PML_FULL;
10909 vmx->nested.pml_full = false;
10910 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10911 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010912 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010913 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010914 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010915
10916 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010917 vmcs12->guest_physical_address = fault->address;
10918}
10919
Peter Feiner995f00a2017-06-30 17:26:32 -070010920static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10921{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010922 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010923}
10924
Nadav Har'El155a97a2013-08-05 11:07:16 +030010925/* Callbacks for nested_ept_init_mmu_context: */
10926
10927static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10928{
10929 /* return the page table to be shadowed - in our case, EPT12 */
10930 return get_vmcs12(vcpu)->ept_pointer;
10931}
10932
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010933static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010934{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010935 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010936 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010937 return 1;
10938
Paolo Bonziniad896af2013-10-02 16:56:14 +020010939 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010940 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010941 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070010942 nested_ept_ad_enabled(vcpu),
10943 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010944 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10945 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10946 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10947
10948 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010949 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010950}
10951
10952static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10953{
10954 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10955}
10956
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010957static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10958 u16 error_code)
10959{
10960 bool inequality, bit;
10961
10962 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10963 inequality =
10964 (error_code & vmcs12->page_fault_error_code_mask) !=
10965 vmcs12->page_fault_error_code_match;
10966 return inequality ^ bit;
10967}
10968
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010969static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10970 struct x86_exception *fault)
10971{
10972 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10973
10974 WARN_ON(!is_guest_mode(vcpu));
10975
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010976 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10977 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010978 vmcs12->vm_exit_intr_error_code = fault->error_code;
10979 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10980 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10981 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10982 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010983 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010984 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010985 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010986}
10987
Paolo Bonzinic9923842017-12-13 14:16:30 +010010988static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10989 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010990
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010991static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010992{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010993 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010994 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010995 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010996 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010997
10998 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010999 /*
11000 * Translate L1 physical address to host physical
11001 * address for vmcs02. Keep the page pinned, so this
11002 * physical address remains valid. We keep a reference
11003 * to it so we can release it later.
11004 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011005 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011006 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011007 vmx->nested.apic_access_page = NULL;
11008 }
11009 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011010 /*
11011 * If translation failed, no matter: This feature asks
11012 * to exit when accessing the given address, and if it
11013 * can never be accessed, this feature won't do
11014 * anything anyway.
11015 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011016 if (!is_error_page(page)) {
11017 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011018 hpa = page_to_phys(vmx->nested.apic_access_page);
11019 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11020 } else {
11021 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11022 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11023 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011024 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011025
11026 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011027 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011028 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011029 vmx->nested.virtual_apic_page = NULL;
11030 }
11031 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011032
11033 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011034 * If translation failed, VM entry will fail because
11035 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11036 * Failing the vm entry is _not_ what the processor
11037 * does but it's basically the only possibility we
11038 * have. We could still enter the guest if CR8 load
11039 * exits are enabled, CR8 store exits are enabled, and
11040 * virtualize APIC access is disabled; in this case
11041 * the processor would never use the TPR shadow and we
11042 * could simply clear the bit from the execution
11043 * control. But such a configuration is useless, so
11044 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011045 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011046 if (!is_error_page(page)) {
11047 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011048 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11049 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11050 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011051 }
11052
Wincy Van705699a2015-02-03 23:58:17 +080011053 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011054 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11055 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011056 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011057 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011058 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011059 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11060 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011061 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011062 vmx->nested.pi_desc_page = page;
11063 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011064 vmx->nested.pi_desc =
11065 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11066 (unsigned long)(vmcs12->posted_intr_desc_addr &
11067 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011068 vmcs_write64(POSTED_INTR_DESC_ADDR,
11069 page_to_phys(vmx->nested.pi_desc_page) +
11070 (unsigned long)(vmcs12->posted_intr_desc_addr &
11071 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011072 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011073 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011074 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11075 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011076 else
11077 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11078 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011079}
11080
Jan Kiszkaf4124502014-03-07 20:03:13 +010011081static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11082{
11083 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11084 struct vcpu_vmx *vmx = to_vmx(vcpu);
11085
11086 if (vcpu->arch.virtual_tsc_khz == 0)
11087 return;
11088
11089 /* Make sure short timeouts reliably trigger an immediate vmexit.
11090 * hrtimer_start does not guarantee this. */
11091 if (preemption_timeout <= 1) {
11092 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11093 return;
11094 }
11095
11096 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11097 preemption_timeout *= 1000000;
11098 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11099 hrtimer_start(&vmx->nested.preemption_timer,
11100 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11101}
11102
Jim Mattson56a20512017-07-06 16:33:06 -070011103static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11104 struct vmcs12 *vmcs12)
11105{
11106 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11107 return 0;
11108
11109 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11110 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11111 return -EINVAL;
11112
11113 return 0;
11114}
11115
Wincy Van3af18d92015-02-03 23:49:31 +080011116static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11117 struct vmcs12 *vmcs12)
11118{
Wincy Van3af18d92015-02-03 23:49:31 +080011119 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11120 return 0;
11121
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011122 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011123 return -EINVAL;
11124
11125 return 0;
11126}
11127
Jim Mattson712b12d2017-08-24 13:24:47 -070011128static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11129 struct vmcs12 *vmcs12)
11130{
11131 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11132 return 0;
11133
11134 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11135 return -EINVAL;
11136
11137 return 0;
11138}
11139
Wincy Van3af18d92015-02-03 23:49:31 +080011140/*
11141 * Merge L0's and L1's MSR bitmap, return false to indicate that
11142 * we do not use the hardware.
11143 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011144static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11145 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011146{
Wincy Van82f0dd42015-02-03 23:57:18 +080011147 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011148 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011149 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011150 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011151 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011152 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011153 *
11154 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11155 * ensures that we do not accidentally generate an L02 MSR bitmap
11156 * from the L12 MSR bitmap that is too permissive.
11157 * 2. That L1 or L2s have actually used the MSR. This avoids
11158 * unnecessarily merging of the bitmap if the MSR is unused. This
11159 * works properly because we only update the L01 MSR bitmap lazily.
11160 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11161 * updated to reflect this when L1 (or its L2s) actually write to
11162 * the MSR.
11163 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011164 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11165 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011166
Paolo Bonzinic9923842017-12-13 14:16:30 +010011167 /* Nothing to do if the MSR bitmap is not in use. */
11168 if (!cpu_has_vmx_msr_bitmap() ||
11169 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11170 return false;
11171
Ashok Raj15d45072018-02-01 22:59:43 +010011172 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011173 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011174 return false;
11175
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011176 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11177 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011178 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011179
Radim Krčmářd048c092016-08-08 20:16:22 +020011180 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011181 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11182 /*
11183 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11184 * just lets the processor take the value from the virtual-APIC page;
11185 * take those 256 bits directly from the L1 bitmap.
11186 */
11187 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11188 unsigned word = msr / BITS_PER_LONG;
11189 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11190 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011191 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011192 } else {
11193 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11194 unsigned word = msr / BITS_PER_LONG;
11195 msr_bitmap_l0[word] = ~0;
11196 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11197 }
11198 }
11199
11200 nested_vmx_disable_intercept_for_msr(
11201 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011202 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011203 MSR_TYPE_W);
11204
11205 if (nested_cpu_has_vid(vmcs12)) {
11206 nested_vmx_disable_intercept_for_msr(
11207 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011208 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011209 MSR_TYPE_W);
11210 nested_vmx_disable_intercept_for_msr(
11211 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011212 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011213 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011214 }
Ashok Raj15d45072018-02-01 22:59:43 +010011215
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011216 if (spec_ctrl)
11217 nested_vmx_disable_intercept_for_msr(
11218 msr_bitmap_l1, msr_bitmap_l0,
11219 MSR_IA32_SPEC_CTRL,
11220 MSR_TYPE_R | MSR_TYPE_W);
11221
Ashok Raj15d45072018-02-01 22:59:43 +010011222 if (pred_cmd)
11223 nested_vmx_disable_intercept_for_msr(
11224 msr_bitmap_l1, msr_bitmap_l0,
11225 MSR_IA32_PRED_CMD,
11226 MSR_TYPE_W);
11227
Wincy Vanf2b93282015-02-03 23:56:03 +080011228 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011229 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011230
11231 return true;
11232}
11233
Liran Alon61ada742018-06-23 02:35:08 +030011234static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11235 struct vmcs12 *vmcs12)
11236{
11237 struct vmcs12 *shadow;
11238 struct page *page;
11239
11240 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11241 vmcs12->vmcs_link_pointer == -1ull)
11242 return;
11243
11244 shadow = get_shadow_vmcs12(vcpu);
11245 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11246
11247 memcpy(shadow, kmap(page), VMCS12_SIZE);
11248
11249 kunmap(page);
11250 kvm_release_page_clean(page);
11251}
11252
11253static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11254 struct vmcs12 *vmcs12)
11255{
11256 struct vcpu_vmx *vmx = to_vmx(vcpu);
11257
11258 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11259 vmcs12->vmcs_link_pointer == -1ull)
11260 return;
11261
11262 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11263 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11264}
11265
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011266static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11267 struct vmcs12 *vmcs12)
11268{
11269 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11270 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11271 return -EINVAL;
11272 else
11273 return 0;
11274}
11275
Wincy Vanf2b93282015-02-03 23:56:03 +080011276static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11277 struct vmcs12 *vmcs12)
11278{
Wincy Van82f0dd42015-02-03 23:57:18 +080011279 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011280 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011281 !nested_cpu_has_vid(vmcs12) &&
11282 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011283 return 0;
11284
11285 /*
11286 * If virtualize x2apic mode is enabled,
11287 * virtualize apic access must be disabled.
11288 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011289 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11290 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011291 return -EINVAL;
11292
Wincy Van608406e2015-02-03 23:57:51 +080011293 /*
11294 * If virtual interrupt delivery is enabled,
11295 * we must exit on external interrupts.
11296 */
11297 if (nested_cpu_has_vid(vmcs12) &&
11298 !nested_exit_on_intr(vcpu))
11299 return -EINVAL;
11300
Wincy Van705699a2015-02-03 23:58:17 +080011301 /*
11302 * bits 15:8 should be zero in posted_intr_nv,
11303 * the descriptor address has been already checked
11304 * in nested_get_vmcs12_pages.
11305 */
11306 if (nested_cpu_has_posted_intr(vmcs12) &&
11307 (!nested_cpu_has_vid(vmcs12) ||
11308 !nested_exit_intr_ack_set(vcpu) ||
11309 vmcs12->posted_intr_nv & 0xff00))
11310 return -EINVAL;
11311
Wincy Vanf2b93282015-02-03 23:56:03 +080011312 /* tpr shadow is needed by all apicv features. */
11313 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11314 return -EINVAL;
11315
11316 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011317}
11318
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011319static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11320 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011321 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011322{
Liran Alone2536742018-06-23 02:35:02 +030011323 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011324 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011325 u64 count, addr;
11326
Liran Alone2536742018-06-23 02:35:02 +030011327 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11328 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011329 WARN_ON(1);
11330 return -EINVAL;
11331 }
11332 if (count == 0)
11333 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011334 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011335 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11336 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011337 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011338 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11339 addr_field, maxphyaddr, count, addr);
11340 return -EINVAL;
11341 }
11342 return 0;
11343}
11344
11345static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11346 struct vmcs12 *vmcs12)
11347{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011348 if (vmcs12->vm_exit_msr_load_count == 0 &&
11349 vmcs12->vm_exit_msr_store_count == 0 &&
11350 vmcs12->vm_entry_msr_load_count == 0)
11351 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011352 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011353 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011354 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011355 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011356 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011357 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011358 return -EINVAL;
11359 return 0;
11360}
11361
Bandan Dasc5f983f2017-05-05 15:25:14 -040011362static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11363 struct vmcs12 *vmcs12)
11364{
11365 u64 address = vmcs12->pml_address;
11366 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11367
11368 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11369 if (!nested_cpu_has_ept(vmcs12) ||
11370 !IS_ALIGNED(address, 4096) ||
11371 address >> maxphyaddr)
11372 return -EINVAL;
11373 }
11374
11375 return 0;
11376}
11377
Liran Alona8a7c022018-06-23 02:35:06 +030011378static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11379 struct vmcs12 *vmcs12)
11380{
11381 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11382 return 0;
11383
11384 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11385 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11386 return -EINVAL;
11387
11388 return 0;
11389}
11390
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011391static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11392 struct vmx_msr_entry *e)
11393{
11394 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011395 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011396 return -EINVAL;
11397 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11398 e->index == MSR_IA32_UCODE_REV)
11399 return -EINVAL;
11400 if (e->reserved != 0)
11401 return -EINVAL;
11402 return 0;
11403}
11404
11405static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11406 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011407{
11408 if (e->index == MSR_FS_BASE ||
11409 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011410 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11411 nested_vmx_msr_check_common(vcpu, e))
11412 return -EINVAL;
11413 return 0;
11414}
11415
11416static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11417 struct vmx_msr_entry *e)
11418{
11419 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11420 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011421 return -EINVAL;
11422 return 0;
11423}
11424
11425/*
11426 * Load guest's/host's msr at nested entry/exit.
11427 * return 0 for success, entry index for failure.
11428 */
11429static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11430{
11431 u32 i;
11432 struct vmx_msr_entry e;
11433 struct msr_data msr;
11434
11435 msr.host_initiated = false;
11436 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011437 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11438 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011439 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011440 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11441 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011442 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011443 }
11444 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011445 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011446 "%s check failed (%u, 0x%x, 0x%x)\n",
11447 __func__, i, e.index, e.reserved);
11448 goto fail;
11449 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011450 msr.index = e.index;
11451 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011452 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011453 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011454 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11455 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011456 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011457 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011458 }
11459 return 0;
11460fail:
11461 return i + 1;
11462}
11463
11464static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11465{
11466 u32 i;
11467 struct vmx_msr_entry e;
11468
11469 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011470 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011471 if (kvm_vcpu_read_guest(vcpu,
11472 gpa + i * sizeof(e),
11473 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011474 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011475 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11476 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011477 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011478 }
11479 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011480 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011481 "%s check failed (%u, 0x%x, 0x%x)\n",
11482 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011483 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011484 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011485 msr_info.host_initiated = false;
11486 msr_info.index = e.index;
11487 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011488 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011489 "%s cannot read MSR (%u, 0x%x)\n",
11490 __func__, i, e.index);
11491 return -EINVAL;
11492 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011493 if (kvm_vcpu_write_guest(vcpu,
11494 gpa + i * sizeof(e) +
11495 offsetof(struct vmx_msr_entry, value),
11496 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011497 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011498 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011499 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011500 return -EINVAL;
11501 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011502 }
11503 return 0;
11504}
11505
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011506static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11507{
11508 unsigned long invalid_mask;
11509
11510 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11511 return (val & invalid_mask) == 0;
11512}
11513
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011514/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011515 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11516 * emulating VM entry into a guest with EPT enabled.
11517 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11518 * is assigned to entry_failure_code on failure.
11519 */
11520static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011521 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011522{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011523 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011524 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011525 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11526 return 1;
11527 }
11528
11529 /*
11530 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11531 * must not be dereferenced.
11532 */
11533 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11534 !nested_ept) {
11535 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11536 *entry_failure_code = ENTRY_FAIL_PDPTE;
11537 return 1;
11538 }
11539 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011540 }
11541
Junaid Shahid50c28f22018-06-27 14:59:11 -070011542 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011543 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011544
11545 vcpu->arch.cr3 = cr3;
11546 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11547
11548 kvm_init_mmu(vcpu, false);
11549
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011550 return 0;
11551}
11552
Jim Mattson6514dc32018-04-26 16:09:12 -070011553static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011554{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011555 struct vcpu_vmx *vmx = to_vmx(vcpu);
11556
11557 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11558 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11559 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11560 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11561 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11562 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11563 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11564 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11565 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11566 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11567 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11568 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11569 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11570 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11571 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11572 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11573 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11574 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11575 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11576 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11577 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11578 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11579 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11580 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11581 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11582 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11583 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11584 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11585 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11586 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11587 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011588
11589 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11590 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11591 vmcs12->guest_pending_dbg_exceptions);
11592 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11593 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11594
11595 if (nested_cpu_has_xsaves(vmcs12))
11596 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11597 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11598
11599 if (cpu_has_vmx_posted_intr())
11600 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11601
11602 /*
11603 * Whether page-faults are trapped is determined by a combination of
11604 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11605 * If enable_ept, L0 doesn't care about page faults and we should
11606 * set all of these to L1's desires. However, if !enable_ept, L0 does
11607 * care about (at least some) page faults, and because it is not easy
11608 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11609 * to exit on each and every L2 page fault. This is done by setting
11610 * MASK=MATCH=0 and (see below) EB.PF=1.
11611 * Note that below we don't need special code to set EB.PF beyond the
11612 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11613 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11614 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11615 */
11616 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11617 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11618 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11619 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11620
11621 /* All VMFUNCs are currently emulated through L0 vmexits. */
11622 if (cpu_has_vmx_vmfunc())
11623 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11624
11625 if (cpu_has_vmx_apicv()) {
11626 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11627 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11628 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11629 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11630 }
11631
11632 /*
11633 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11634 * Some constant fields are set here by vmx_set_constant_host_state().
11635 * Other fields are different per CPU, and will be set later when
11636 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11637 */
11638 vmx_set_constant_host_state(vmx);
11639
11640 /*
11641 * Set the MSR load/store lists to match L0's settings.
11642 */
11643 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11644 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11645 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11646 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11647 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11648
11649 set_cr4_guest_host_mask(vmx);
11650
11651 if (vmx_mpx_supported())
11652 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11653
11654 if (enable_vpid) {
11655 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11656 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11657 else
11658 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11659 }
11660
11661 /*
11662 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11663 */
11664 if (enable_ept) {
11665 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11666 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11667 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11668 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11669 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011670
11671 if (cpu_has_vmx_msr_bitmap())
11672 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011673}
11674
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011675/*
11676 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11677 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011678 * 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 +030011679 * guest in a way that will both be appropriate to L1's requests, and our
11680 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11681 * function also has additional necessary side-effects, like setting various
11682 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011683 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11684 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011685 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011686static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011687 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011688{
11689 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011690 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011691
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011692 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011693 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011694 vmx->nested.dirty_vmcs12 = false;
11695 }
11696
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011697 /*
11698 * First, the fields that are shadowed. This must be kept in sync
11699 * with vmx_shadow_fields.h.
11700 */
11701
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011702 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011703 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011704 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011705 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11706 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011707
11708 /*
11709 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11710 * HOST_FS_BASE, HOST_GS_BASE.
11711 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011712
Jim Mattson6514dc32018-04-26 16:09:12 -070011713 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011714 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011715 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11716 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11717 } else {
11718 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11719 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11720 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011721 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011722 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11723 vmcs12->vm_entry_intr_info_field);
11724 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11725 vmcs12->vm_entry_exception_error_code);
11726 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11727 vmcs12->vm_entry_instruction_len);
11728 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11729 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011730 vmx->loaded_vmcs->nmi_known_unmasked =
11731 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011732 } else {
11733 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11734 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011735 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011736
Jan Kiszkaf4124502014-03-07 20:03:13 +010011737 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011738
Paolo Bonzini93140062016-07-06 13:23:51 +020011739 /* Preemption timer setting is only taken from vmcs01. */
11740 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11741 exec_control |= vmcs_config.pin_based_exec_ctrl;
11742 if (vmx->hv_deadline_tsc == -1)
11743 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11744
11745 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011746 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011747 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11748 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011749 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011750 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011751 }
Wincy Van705699a2015-02-03 23:58:17 +080011752
Jan Kiszkaf4124502014-03-07 20:03:13 +010011753 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011754
Jan Kiszkaf4124502014-03-07 20:03:13 +010011755 vmx->nested.preemption_timer_expired = false;
11756 if (nested_cpu_has_preemption_timer(vmcs12))
11757 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011758
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011759 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011760 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011761
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011762 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011763 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011764 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011765 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011766 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011767 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011768 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11769 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011770 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011771 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11772 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11773 ~SECONDARY_EXEC_ENABLE_PML;
11774 exec_control |= vmcs12_exec_ctrl;
11775 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011776
Liran Alon32c7acf2018-06-23 02:35:11 +030011777 /* VMCS shadowing for L2 is emulated for now */
11778 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
11779
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011780 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011781 vmcs_write16(GUEST_INTR_STATUS,
11782 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011783
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011784 /*
11785 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11786 * nested_get_vmcs12_pages will either fix it up or
11787 * remove the VM execution control.
11788 */
11789 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11790 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11791
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011792 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11793 }
11794
Jim Mattson83bafef2016-10-04 10:48:38 -070011795 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011796 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11797 * entry, but only if the current (host) sp changed from the value
11798 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11799 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11800 * here we just force the write to happen on entry.
11801 */
11802 vmx->host_rsp = 0;
11803
11804 exec_control = vmx_exec_control(vmx); /* L0's desires */
11805 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11806 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11807 exec_control &= ~CPU_BASED_TPR_SHADOW;
11808 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011809
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011810 /*
11811 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11812 * nested_get_vmcs12_pages can't fix it up, the illegal value
11813 * will result in a VM entry failure.
11814 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011815 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011816 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011817 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011818 } else {
11819#ifdef CONFIG_X86_64
11820 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11821 CPU_BASED_CR8_STORE_EXITING;
11822#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011823 }
11824
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011825 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011826 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11827 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011828 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011829 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11830 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11831
11832 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11833
11834 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11835 * bitwise-or of what L1 wants to trap for L2, and what we want to
11836 * trap. Note that CR0.TS also needs updating - we do this later.
11837 */
11838 update_exception_bitmap(vcpu);
11839 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11840 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11841
Nadav Har'El8049d652013-08-05 11:07:06 +030011842 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11843 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11844 * bits are further modified by vmx_set_efer() below.
11845 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011846 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011847
11848 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11849 * emulated by vmx_set_efer(), below.
11850 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011851 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011852 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11853 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011854 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11855
Jim Mattson6514dc32018-04-26 16:09:12 -070011856 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011857 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011858 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011859 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011860 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011861 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011862 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011863
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011864 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11865
Peter Feinerc95ba922016-08-17 09:36:47 -070011866 if (kvm_has_tsc_control)
11867 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011868
11869 if (enable_vpid) {
11870 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011871 * There is no direct mapping between vpid02 and vpid12, the
11872 * vpid02 is per-vCPU for L0 and reused while the value of
11873 * vpid12 is changed w/ one invvpid during nested vmentry.
11874 * The vpid12 is allocated by L1 for L2, so it will not
11875 * influence global bitmap(for vpid01 and vpid02 allocation)
11876 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011877 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011878 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011879 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11880 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011881 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011882 }
11883 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011884 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011885 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011886 }
11887
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011888 if (enable_pml) {
11889 /*
11890 * Conceptually we want to copy the PML address and index from
11891 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11892 * since we always flush the log on each vmexit, this happens
11893 * to be equivalent to simply resetting the fields in vmcs02.
11894 */
11895 ASSERT(vmx->pml_pg);
11896 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11897 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11898 }
11899
Nadav Har'El155a97a2013-08-05 11:07:16 +030011900 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011901 if (nested_ept_init_mmu_context(vcpu)) {
11902 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11903 return 1;
11904 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011905 } else if (nested_cpu_has2(vmcs12,
11906 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011907 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011908 }
11909
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011910 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011911 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11912 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011913 * The CR0_READ_SHADOW is what L2 should have expected to read given
11914 * the specifications by L1; It's not enough to take
11915 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11916 * have more bits than L1 expected.
11917 */
11918 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11919 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11920
11921 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11922 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11923
Jim Mattson6514dc32018-04-26 16:09:12 -070011924 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011925 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011926 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11927 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11928 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11929 else
11930 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11931 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11932 vmx_set_efer(vcpu, vcpu->arch.efer);
11933
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011934 /*
11935 * Guest state is invalid and unrestricted guest is disabled,
11936 * which means L1 attempted VMEntry to L2 with invalid state.
11937 * Fail the VMEntry.
11938 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011939 if (vmx->emulation_required) {
11940 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011941 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011942 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011943
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011944 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011945 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011946 entry_failure_code))
11947 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011948
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011949 if (!enable_ept)
11950 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11951
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011952 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11953 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011954 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011955}
11956
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011957static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11958{
11959 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11960 nested_cpu_has_virtual_nmis(vmcs12))
11961 return -EINVAL;
11962
11963 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11964 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11965 return -EINVAL;
11966
11967 return 0;
11968}
11969
Jim Mattsonca0bde22016-11-30 12:03:46 -080011970static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11971{
11972 struct vcpu_vmx *vmx = to_vmx(vcpu);
11973
11974 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11975 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11976 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11977
Jim Mattson56a20512017-07-06 16:33:06 -070011978 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11979 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11980
Jim Mattsonca0bde22016-11-30 12:03:46 -080011981 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11982 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11983
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011984 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11985 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11986
Jim Mattson712b12d2017-08-24 13:24:47 -070011987 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11988 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11989
Jim Mattsonca0bde22016-11-30 12:03:46 -080011990 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11991 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11992
11993 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11994 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11995
Bandan Dasc5f983f2017-05-05 15:25:14 -040011996 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11997 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11998
Liran Alona8a7c022018-06-23 02:35:06 +030011999 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12000 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12001
Jim Mattsonca0bde22016-11-30 12:03:46 -080012002 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012003 vmx->nested.msrs.procbased_ctls_low,
12004 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012005 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12006 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012007 vmx->nested.msrs.secondary_ctls_low,
12008 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012009 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012010 vmx->nested.msrs.pinbased_ctls_low,
12011 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012012 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012013 vmx->nested.msrs.exit_ctls_low,
12014 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012015 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012016 vmx->nested.msrs.entry_ctls_low,
12017 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012018 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12019
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012020 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012021 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12022
Bandan Das41ab9372017-08-03 15:54:43 -040012023 if (nested_cpu_has_vmfunc(vmcs12)) {
12024 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012025 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012026 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12027
12028 if (nested_cpu_has_eptp_switching(vmcs12)) {
12029 if (!nested_cpu_has_ept(vmcs12) ||
12030 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12031 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12032 }
12033 }
Bandan Das27c42a12017-08-03 15:54:42 -040012034
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012035 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12036 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12037
Jim Mattsonca0bde22016-11-30 12:03:46 -080012038 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12039 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12040 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12041 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12042
Marc Orr04473782018-06-20 17:21:29 -070012043 /*
12044 * From the Intel SDM, volume 3:
12045 * Fields relevant to VM-entry event injection must be set properly.
12046 * These fields are the VM-entry interruption-information field, the
12047 * VM-entry exception error code, and the VM-entry instruction length.
12048 */
12049 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12050 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12051 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12052 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12053 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12054 bool should_have_error_code;
12055 bool urg = nested_cpu_has2(vmcs12,
12056 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12057 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12058
12059 /* VM-entry interruption-info field: interruption type */
12060 if (intr_type == INTR_TYPE_RESERVED ||
12061 (intr_type == INTR_TYPE_OTHER_EVENT &&
12062 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12063 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12064
12065 /* VM-entry interruption-info field: vector */
12066 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12067 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12068 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12069 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12070
12071 /* VM-entry interruption-info field: deliver error code */
12072 should_have_error_code =
12073 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12074 x86_exception_has_error_code(vector);
12075 if (has_error_code != should_have_error_code)
12076 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12077
12078 /* VM-entry exception error code */
12079 if (has_error_code &&
12080 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12081 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12082
12083 /* VM-entry interruption-info field: reserved bits */
12084 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12085 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12086
12087 /* VM-entry instruction length */
12088 switch (intr_type) {
12089 case INTR_TYPE_SOFT_EXCEPTION:
12090 case INTR_TYPE_SOFT_INTR:
12091 case INTR_TYPE_PRIV_SW_EXCEPTION:
12092 if ((vmcs12->vm_entry_instruction_len > 15) ||
12093 (vmcs12->vm_entry_instruction_len == 0 &&
12094 !nested_cpu_has_zero_length_injection(vcpu)))
12095 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12096 }
12097 }
12098
Jim Mattsonca0bde22016-11-30 12:03:46 -080012099 return 0;
12100}
12101
Liran Alonf145d902018-06-23 02:35:07 +030012102static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12103 struct vmcs12 *vmcs12)
12104{
12105 int r;
12106 struct page *page;
12107 struct vmcs12 *shadow;
12108
12109 if (vmcs12->vmcs_link_pointer == -1ull)
12110 return 0;
12111
12112 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12113 return -EINVAL;
12114
12115 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12116 if (is_error_page(page))
12117 return -EINVAL;
12118
12119 r = 0;
12120 shadow = kmap(page);
12121 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12122 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12123 r = -EINVAL;
12124 kunmap(page);
12125 kvm_release_page_clean(page);
12126 return r;
12127}
12128
Jim Mattsonca0bde22016-11-30 12:03:46 -080012129static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12130 u32 *exit_qual)
12131{
12132 bool ia32e;
12133
12134 *exit_qual = ENTRY_FAIL_DEFAULT;
12135
12136 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12137 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12138 return 1;
12139
Liran Alonf145d902018-06-23 02:35:07 +030012140 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012141 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12142 return 1;
12143 }
12144
12145 /*
12146 * If the load IA32_EFER VM-entry control is 1, the following checks
12147 * are performed on the field for the IA32_EFER MSR:
12148 * - Bits reserved in the IA32_EFER MSR must be 0.
12149 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12150 * the IA-32e mode guest VM-exit control. It must also be identical
12151 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12152 * CR0.PG) is 1.
12153 */
12154 if (to_vmx(vcpu)->nested.nested_run_pending &&
12155 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12156 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12157 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12158 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12159 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12160 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12161 return 1;
12162 }
12163
12164 /*
12165 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12166 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12167 * the values of the LMA and LME bits in the field must each be that of
12168 * the host address-space size VM-exit control.
12169 */
12170 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12171 ia32e = (vmcs12->vm_exit_controls &
12172 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12173 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12174 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12175 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12176 return 1;
12177 }
12178
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012179 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12180 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12181 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12182 return 1;
12183
Jim Mattsonca0bde22016-11-30 12:03:46 -080012184 return 0;
12185}
12186
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012187/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012188 * If exit_qual is NULL, this is being called from state restore (either RSM
12189 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012190 */
12191static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012192{
12193 struct vcpu_vmx *vmx = to_vmx(vcpu);
12194 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012195 bool from_vmentry = !!exit_qual;
12196 u32 dummy_exit_qual;
12197 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012198
Jim Mattson858e25c2016-11-30 12:03:47 -080012199 enter_guest_mode(vcpu);
12200
12201 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12202 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12203
Jim Mattsonde3a0022017-11-27 17:22:25 -060012204 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012205 vmx_segment_cache_clear(vmx);
12206
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012207 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12208 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12209
12210 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012211 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012212 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012213
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012214 if (from_vmentry) {
12215 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012216
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012217 r = EXIT_REASON_MSR_LOAD_FAIL;
12218 *exit_qual = nested_vmx_load_msr(vcpu,
12219 vmcs12->vm_entry_msr_load_addr,
12220 vmcs12->vm_entry_msr_load_count);
12221 if (*exit_qual)
12222 goto fail;
12223 } else {
12224 /*
12225 * The MMU is not initialized to point at the right entities yet and
12226 * "get pages" would need to read data from the guest (i.e. we will
12227 * need to perform gpa to hpa translation). Request a call
12228 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12229 * have already been set at vmentry time and should not be reset.
12230 */
12231 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12232 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012233
Jim Mattson858e25c2016-11-30 12:03:47 -080012234 /*
12235 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12236 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12237 * returned as far as L1 is concerned. It will only return (and set
12238 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12239 */
12240 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012241
12242fail:
12243 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12244 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12245 leave_guest_mode(vcpu);
12246 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012247 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012248}
12249
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012250/*
12251 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12252 * for running an L2 nested guest.
12253 */
12254static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12255{
12256 struct vmcs12 *vmcs12;
12257 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012258 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012259 u32 exit_qual;
12260 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012261
Kyle Hueyeb277562016-11-29 12:40:39 -080012262 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012263 return 1;
12264
Kyle Hueyeb277562016-11-29 12:40:39 -080012265 if (!nested_vmx_check_vmcs12(vcpu))
12266 goto out;
12267
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012268 vmcs12 = get_vmcs12(vcpu);
12269
Liran Alona6192d42018-06-23 02:35:04 +030012270 /*
12271 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12272 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12273 * rather than RFLAGS.ZF, and no error number is stored to the
12274 * VM-instruction error field.
12275 */
12276 if (vmcs12->hdr.shadow_vmcs) {
12277 nested_vmx_failInvalid(vcpu);
12278 goto out;
12279 }
12280
Abel Gordon012f83c2013-04-18 14:39:25 +030012281 if (enable_shadow_vmcs)
12282 copy_shadow_to_vmcs12(vmx);
12283
Nadav Har'El7c177932011-05-25 23:12:04 +030012284 /*
12285 * The nested entry process starts with enforcing various prerequisites
12286 * on vmcs12 as required by the Intel SDM, and act appropriately when
12287 * they fail: As the SDM explains, some conditions should cause the
12288 * instruction to fail, while others will cause the instruction to seem
12289 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12290 * To speed up the normal (success) code path, we should avoid checking
12291 * for misconfigurations which will anyway be caught by the processor
12292 * when using the merged vmcs02.
12293 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012294 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12295 nested_vmx_failValid(vcpu,
12296 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12297 goto out;
12298 }
12299
Nadav Har'El7c177932011-05-25 23:12:04 +030012300 if (vmcs12->launch_state == launch) {
12301 nested_vmx_failValid(vcpu,
12302 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12303 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012304 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012305 }
12306
Jim Mattsonca0bde22016-11-30 12:03:46 -080012307 ret = check_vmentry_prereqs(vcpu, vmcs12);
12308 if (ret) {
12309 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012310 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012311 }
12312
Nadav Har'El7c177932011-05-25 23:12:04 +030012313 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012314 * After this point, the trap flag no longer triggers a singlestep trap
12315 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12316 * This is not 100% correct; for performance reasons, we delegate most
12317 * of the checks on host state to the processor. If those fail,
12318 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012319 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012320 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012321
Jim Mattsonca0bde22016-11-30 12:03:46 -080012322 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12323 if (ret) {
12324 nested_vmx_entry_failure(vcpu, vmcs12,
12325 EXIT_REASON_INVALID_STATE, exit_qual);
12326 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012327 }
12328
12329 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012330 * We're finally done with prerequisite checking, and can start with
12331 * the nested entry.
12332 */
12333
Jim Mattson6514dc32018-04-26 16:09:12 -070012334 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012335 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012336 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012337 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012338 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012339 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012340 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012341
Chao Gao135a06c2018-02-11 10:06:30 +080012342 /*
Liran Alon61ada742018-06-23 02:35:08 +030012343 * Must happen outside of enter_vmx_non_root_mode() as it will
12344 * also be used as part of restoring nVMX state for
12345 * snapshot restore (migration).
12346 *
12347 * In this flow, it is assumed that vmcs12 cache was
12348 * trasferred as part of captured nVMX state and should
12349 * therefore not be read from guest memory (which may not
12350 * exist on destination host yet).
12351 */
12352 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12353
12354 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012355 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12356 * by event injection, halt vcpu.
12357 */
12358 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012359 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12360 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012361 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012362 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012363 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012364
12365out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012366 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012367}
12368
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012369/*
12370 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12371 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12372 * This function returns the new value we should put in vmcs12.guest_cr0.
12373 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12374 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12375 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12376 * didn't trap the bit, because if L1 did, so would L0).
12377 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12378 * been modified by L2, and L1 knows it. So just leave the old value of
12379 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12380 * isn't relevant, because if L0 traps this bit it can set it to anything.
12381 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12382 * changed these bits, and therefore they need to be updated, but L0
12383 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12384 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12385 */
12386static inline unsigned long
12387vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12388{
12389 return
12390 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12391 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12392 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12393 vcpu->arch.cr0_guest_owned_bits));
12394}
12395
12396static inline unsigned long
12397vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12398{
12399 return
12400 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12401 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12402 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12403 vcpu->arch.cr4_guest_owned_bits));
12404}
12405
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012406static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12407 struct vmcs12 *vmcs12)
12408{
12409 u32 idt_vectoring;
12410 unsigned int nr;
12411
Wanpeng Li664f8e22017-08-24 03:35:09 -070012412 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012413 nr = vcpu->arch.exception.nr;
12414 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12415
12416 if (kvm_exception_is_soft(nr)) {
12417 vmcs12->vm_exit_instruction_len =
12418 vcpu->arch.event_exit_inst_len;
12419 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12420 } else
12421 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12422
12423 if (vcpu->arch.exception.has_error_code) {
12424 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12425 vmcs12->idt_vectoring_error_code =
12426 vcpu->arch.exception.error_code;
12427 }
12428
12429 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012430 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012431 vmcs12->idt_vectoring_info_field =
12432 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012433 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012434 nr = vcpu->arch.interrupt.nr;
12435 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12436
12437 if (vcpu->arch.interrupt.soft) {
12438 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12439 vmcs12->vm_entry_instruction_len =
12440 vcpu->arch.event_exit_inst_len;
12441 } else
12442 idt_vectoring |= INTR_TYPE_EXT_INTR;
12443
12444 vmcs12->idt_vectoring_info_field = idt_vectoring;
12445 }
12446}
12447
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012448static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12449{
12450 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012451 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012452 bool block_nested_events =
12453 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012454
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012455 if (vcpu->arch.exception.pending &&
12456 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012457 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012458 return -EBUSY;
12459 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012460 return 0;
12461 }
12462
Jan Kiszkaf4124502014-03-07 20:03:13 +010012463 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12464 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012465 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012466 return -EBUSY;
12467 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12468 return 0;
12469 }
12470
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012471 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012472 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012473 return -EBUSY;
12474 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12475 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12476 INTR_INFO_VALID_MASK, 0);
12477 /*
12478 * The NMI-triggered VM exit counts as injection:
12479 * clear this one and block further NMIs.
12480 */
12481 vcpu->arch.nmi_pending = 0;
12482 vmx_set_nmi_mask(vcpu, true);
12483 return 0;
12484 }
12485
12486 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12487 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012488 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012489 return -EBUSY;
12490 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012491 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012492 }
12493
David Hildenbrand6342c502017-01-25 11:58:58 +010012494 vmx_complete_nested_posted_interrupt(vcpu);
12495 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012496}
12497
Jan Kiszkaf4124502014-03-07 20:03:13 +010012498static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12499{
12500 ktime_t remaining =
12501 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12502 u64 value;
12503
12504 if (ktime_to_ns(remaining) <= 0)
12505 return 0;
12506
12507 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12508 do_div(value, 1000000);
12509 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12510}
12511
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012512/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012513 * Update the guest state fields of vmcs12 to reflect changes that
12514 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12515 * VM-entry controls is also updated, since this is really a guest
12516 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012517 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012518static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012519{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012520 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12521 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12522
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012523 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12524 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12525 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12526
12527 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12528 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12529 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12530 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12531 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12532 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12533 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12534 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12535 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12536 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12537 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12538 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12539 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12540 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12541 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12542 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12543 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12544 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12545 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12546 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12547 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12548 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12549 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12550 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12551 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12552 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12553 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12554 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12555 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12556 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12557 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12558 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12559 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12560 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12561 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12562 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12563
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012564 vmcs12->guest_interruptibility_info =
12565 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12566 vmcs12->guest_pending_dbg_exceptions =
12567 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012568 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12569 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12570 else
12571 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012572
Jan Kiszkaf4124502014-03-07 20:03:13 +010012573 if (nested_cpu_has_preemption_timer(vmcs12)) {
12574 if (vmcs12->vm_exit_controls &
12575 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12576 vmcs12->vmx_preemption_timer_value =
12577 vmx_get_preemption_timer_value(vcpu);
12578 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12579 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012580
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012581 /*
12582 * In some cases (usually, nested EPT), L2 is allowed to change its
12583 * own CR3 without exiting. If it has changed it, we must keep it.
12584 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12585 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12586 *
12587 * Additionally, restore L2's PDPTR to vmcs12.
12588 */
12589 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012590 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012591 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12592 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12593 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12594 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12595 }
12596
Jim Mattsond281e132017-06-01 12:44:46 -070012597 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012598
Wincy Van608406e2015-02-03 23:57:51 +080012599 if (nested_cpu_has_vid(vmcs12))
12600 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12601
Jan Kiszkac18911a2013-03-13 16:06:41 +010012602 vmcs12->vm_entry_controls =
12603 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012604 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012605
Jan Kiszka2996fca2014-06-16 13:59:43 +020012606 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12607 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12608 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12609 }
12610
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012611 /* TODO: These cannot have changed unless we have MSR bitmaps and
12612 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012613 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012614 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012615 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12616 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012617 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12618 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12619 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012620 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012621 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012622}
12623
12624/*
12625 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12626 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12627 * and this function updates it to reflect the changes to the guest state while
12628 * L2 was running (and perhaps made some exits which were handled directly by L0
12629 * without going back to L1), and to reflect the exit reason.
12630 * Note that we do not have to copy here all VMCS fields, just those that
12631 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12632 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12633 * which already writes to vmcs12 directly.
12634 */
12635static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12636 u32 exit_reason, u32 exit_intr_info,
12637 unsigned long exit_qualification)
12638{
12639 /* update guest state fields: */
12640 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012641
12642 /* update exit information fields: */
12643
Jan Kiszka533558b2014-01-04 18:47:20 +010012644 vmcs12->vm_exit_reason = exit_reason;
12645 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012646 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012647
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012648 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012649 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12650 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12651
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012652 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012653 vmcs12->launch_state = 1;
12654
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012655 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12656 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012657 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012658
12659 /*
12660 * Transfer the event that L0 or L1 may wanted to inject into
12661 * L2 to IDT_VECTORING_INFO_FIELD.
12662 */
12663 vmcs12_save_pending_event(vcpu, vmcs12);
12664 }
12665
12666 /*
12667 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12668 * preserved above and would only end up incorrectly in L1.
12669 */
12670 vcpu->arch.nmi_injected = false;
12671 kvm_clear_exception_queue(vcpu);
12672 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012673}
12674
Wanpeng Li5af41572017-11-05 16:54:49 -080012675static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12676 struct vmcs12 *vmcs12)
12677{
12678 u32 entry_failure_code;
12679
12680 nested_ept_uninit_mmu_context(vcpu);
12681
12682 /*
12683 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12684 * couldn't have changed.
12685 */
12686 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12687 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12688
12689 if (!enable_ept)
12690 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12691}
12692
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012693/*
12694 * A part of what we need to when the nested L2 guest exits and we want to
12695 * run its L1 parent, is to reset L1's guest state to the host state specified
12696 * in vmcs12.
12697 * This function is to be called not only on normal nested exit, but also on
12698 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12699 * Failures During or After Loading Guest State").
12700 * This function should be called when the active VMCS is L1's (vmcs01).
12701 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012702static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12703 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012704{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012705 struct kvm_segment seg;
12706
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012707 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12708 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012709 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012710 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12711 else
12712 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12713 vmx_set_efer(vcpu, vcpu->arch.efer);
12714
12715 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12716 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012717 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012718 /*
12719 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012720 * actually changed, because vmx_set_cr0 refers to efer set above.
12721 *
12722 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12723 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012724 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012725 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012726 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012727
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012728 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012729 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012730 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012731
Wanpeng Li5af41572017-11-05 16:54:49 -080012732 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012733
Liran Alon6f1e03b2018-05-22 17:16:14 +030012734 /*
12735 * If vmcs01 don't use VPID, CPU flushes TLB on every
12736 * VMEntry/VMExit. Thus, no need to flush TLB.
12737 *
12738 * If vmcs12 uses VPID, TLB entries populated by L2 are
12739 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12740 * with vmx->vpid. Thus, no need to flush TLB.
12741 *
12742 * Therefore, flush TLB only in case vmcs01 uses VPID and
12743 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12744 * are both tagged with vmx->vpid.
12745 */
12746 if (enable_vpid &&
12747 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012748 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012749 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012750
12751 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12752 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12753 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12754 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12755 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012756 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12757 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012758
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012759 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12760 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12761 vmcs_write64(GUEST_BNDCFGS, 0);
12762
Jan Kiszka44811c02013-08-04 17:17:27 +020012763 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012764 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012765 vcpu->arch.pat = vmcs12->host_ia32_pat;
12766 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012767 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12768 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12769 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012770
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012771 /* Set L1 segment info according to Intel SDM
12772 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12773 seg = (struct kvm_segment) {
12774 .base = 0,
12775 .limit = 0xFFFFFFFF,
12776 .selector = vmcs12->host_cs_selector,
12777 .type = 11,
12778 .present = 1,
12779 .s = 1,
12780 .g = 1
12781 };
12782 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12783 seg.l = 1;
12784 else
12785 seg.db = 1;
12786 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12787 seg = (struct kvm_segment) {
12788 .base = 0,
12789 .limit = 0xFFFFFFFF,
12790 .type = 3,
12791 .present = 1,
12792 .s = 1,
12793 .db = 1,
12794 .g = 1
12795 };
12796 seg.selector = vmcs12->host_ds_selector;
12797 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12798 seg.selector = vmcs12->host_es_selector;
12799 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12800 seg.selector = vmcs12->host_ss_selector;
12801 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12802 seg.selector = vmcs12->host_fs_selector;
12803 seg.base = vmcs12->host_fs_base;
12804 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12805 seg.selector = vmcs12->host_gs_selector;
12806 seg.base = vmcs12->host_gs_base;
12807 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12808 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012809 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012810 .limit = 0x67,
12811 .selector = vmcs12->host_tr_selector,
12812 .type = 11,
12813 .present = 1
12814 };
12815 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12816
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012817 kvm_set_dr(vcpu, 7, 0x400);
12818 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012819
Wincy Van3af18d92015-02-03 23:49:31 +080012820 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012821 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012822
Wincy Vanff651cb2014-12-11 08:52:58 +030012823 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12824 vmcs12->vm_exit_msr_load_count))
12825 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012826}
12827
12828/*
12829 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12830 * and modify vmcs12 to make it see what it would expect to see there if
12831 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12832 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012833static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12834 u32 exit_intr_info,
12835 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012836{
12837 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012838 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12839
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012840 /* trying to cancel vmlaunch/vmresume is a bug */
12841 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12842
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012843 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012844 * The only expected VM-instruction error is "VM entry with
12845 * invalid control field(s)." Anything else indicates a
12846 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012847 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012848 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12849 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12850
12851 leave_guest_mode(vcpu);
12852
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012853 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12854 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12855
Jim Mattson4f350c62017-09-14 16:31:44 -070012856 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012857 if (exit_reason == -1)
12858 sync_vmcs12(vcpu, vmcs12);
12859 else
12860 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12861 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012862
Liran Alon61ada742018-06-23 02:35:08 +030012863 /*
12864 * Must happen outside of sync_vmcs12() as it will
12865 * also be used to capture vmcs12 cache as part of
12866 * capturing nVMX state for snapshot (migration).
12867 *
12868 * Otherwise, this flush will dirty guest memory at a
12869 * point it is already assumed by user-space to be
12870 * immutable.
12871 */
12872 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12873
Jim Mattson4f350c62017-09-14 16:31:44 -070012874 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12875 vmcs12->vm_exit_msr_store_count))
12876 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012877 }
12878
Jim Mattson4f350c62017-09-14 16:31:44 -070012879 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012880 vm_entry_controls_reset_shadow(vmx);
12881 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012882 vmx_segment_cache_clear(vmx);
12883
Paolo Bonzini93140062016-07-06 13:23:51 +020012884 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012885 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12886 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012887 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012888 if (vmx->hv_deadline_tsc == -1)
12889 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12890 PIN_BASED_VMX_PREEMPTION_TIMER);
12891 else
12892 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12893 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012894 if (kvm_has_tsc_control)
12895 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012896
Jim Mattson8d860bb2018-05-09 16:56:05 -040012897 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12898 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12899 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012900 } else if (!nested_cpu_has_ept(vmcs12) &&
12901 nested_cpu_has2(vmcs12,
12902 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012903 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012904 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012905
12906 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12907 vmx->host_rsp = 0;
12908
12909 /* Unpin physical memory we referred to in vmcs02 */
12910 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012911 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012912 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012913 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012914 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012915 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012916 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012917 }
Wincy Van705699a2015-02-03 23:58:17 +080012918 if (vmx->nested.pi_desc_page) {
12919 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012920 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012921 vmx->nested.pi_desc_page = NULL;
12922 vmx->nested.pi_desc = NULL;
12923 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012924
12925 /*
Tang Chen38b99172014-09-24 15:57:54 +080012926 * We are now running in L2, mmu_notifier will force to reload the
12927 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12928 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012929 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012930
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012931 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012932 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012933
12934 /* in case we halted in L2 */
12935 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012936
12937 if (likely(!vmx->fail)) {
12938 /*
12939 * TODO: SDM says that with acknowledge interrupt on
12940 * exit, bit 31 of the VM-exit interrupt information
12941 * (valid interrupt) is always set to 1 on
12942 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12943 * need kvm_cpu_has_interrupt(). See the commit
12944 * message for details.
12945 */
12946 if (nested_exit_intr_ack_set(vcpu) &&
12947 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12948 kvm_cpu_has_interrupt(vcpu)) {
12949 int irq = kvm_cpu_get_interrupt(vcpu);
12950 WARN_ON(irq < 0);
12951 vmcs12->vm_exit_intr_info = irq |
12952 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12953 }
12954
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012955 if (exit_reason != -1)
12956 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12957 vmcs12->exit_qualification,
12958 vmcs12->idt_vectoring_info_field,
12959 vmcs12->vm_exit_intr_info,
12960 vmcs12->vm_exit_intr_error_code,
12961 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012962
12963 load_vmcs12_host_state(vcpu, vmcs12);
12964
12965 return;
12966 }
12967
12968 /*
12969 * After an early L2 VM-entry failure, we're now back
12970 * in L1 which thinks it just finished a VMLAUNCH or
12971 * VMRESUME instruction, so we need to set the failure
12972 * flag and the VM-instruction error field of the VMCS
12973 * accordingly.
12974 */
12975 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012976
12977 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12978
Jim Mattson4f350c62017-09-14 16:31:44 -070012979 /*
12980 * The emulated instruction was already skipped in
12981 * nested_vmx_run, but the updated RIP was never
12982 * written back to the vmcs01.
12983 */
12984 skip_emulated_instruction(vcpu);
12985 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012986}
12987
Nadav Har'El7c177932011-05-25 23:12:04 +030012988/*
Jan Kiszka42124922014-01-04 18:47:19 +010012989 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12990 */
12991static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12992{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012993 if (is_guest_mode(vcpu)) {
12994 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012995 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012996 }
Jan Kiszka42124922014-01-04 18:47:19 +010012997 free_nested(to_vmx(vcpu));
12998}
12999
13000/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013001 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13002 * 23.7 "VM-entry failures during or after loading guest state" (this also
13003 * lists the acceptable exit-reason and exit-qualification parameters).
13004 * It should only be called before L2 actually succeeded to run, and when
13005 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13006 */
13007static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13008 struct vmcs12 *vmcs12,
13009 u32 reason, unsigned long qualification)
13010{
13011 load_vmcs12_host_state(vcpu, vmcs12);
13012 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13013 vmcs12->exit_qualification = qualification;
13014 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013015 if (enable_shadow_vmcs)
13016 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013017}
13018
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013019static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13020 struct x86_instruction_info *info,
13021 enum x86_intercept_stage stage)
13022{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013023 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13024 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13025
13026 /*
13027 * RDPID causes #UD if disabled through secondary execution controls.
13028 * Because it is marked as EmulateOnUD, we need to intercept it here.
13029 */
13030 if (info->intercept == x86_intercept_rdtscp &&
13031 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13032 ctxt->exception.vector = UD_VECTOR;
13033 ctxt->exception.error_code_valid = false;
13034 return X86EMUL_PROPAGATE_FAULT;
13035 }
13036
13037 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013038 return X86EMUL_CONTINUE;
13039}
13040
Yunhong Jiang64672c92016-06-13 14:19:59 -070013041#ifdef CONFIG_X86_64
13042/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13043static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13044 u64 divisor, u64 *result)
13045{
13046 u64 low = a << shift, high = a >> (64 - shift);
13047
13048 /* To avoid the overflow on divq */
13049 if (high >= divisor)
13050 return 1;
13051
13052 /* Low hold the result, high hold rem which is discarded */
13053 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13054 "rm" (divisor), "0" (low), "1" (high));
13055 *result = low;
13056
13057 return 0;
13058}
13059
13060static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13061{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013062 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013063 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013064
13065 if (kvm_mwait_in_guest(vcpu->kvm))
13066 return -EOPNOTSUPP;
13067
13068 vmx = to_vmx(vcpu);
13069 tscl = rdtsc();
13070 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13071 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013072 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13073
13074 if (delta_tsc > lapic_timer_advance_cycles)
13075 delta_tsc -= lapic_timer_advance_cycles;
13076 else
13077 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013078
13079 /* Convert to host delta tsc if tsc scaling is enabled */
13080 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13081 u64_shl_div_u64(delta_tsc,
13082 kvm_tsc_scaling_ratio_frac_bits,
13083 vcpu->arch.tsc_scaling_ratio,
13084 &delta_tsc))
13085 return -ERANGE;
13086
13087 /*
13088 * If the delta tsc can't fit in the 32 bit after the multi shift,
13089 * we can't use the preemption timer.
13090 * It's possible that it fits on later vmentries, but checking
13091 * on every vmentry is costly so we just use an hrtimer.
13092 */
13093 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13094 return -ERANGE;
13095
13096 vmx->hv_deadline_tsc = tscl + delta_tsc;
13097 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13098 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070013099
13100 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013101}
13102
13103static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13104{
13105 struct vcpu_vmx *vmx = to_vmx(vcpu);
13106 vmx->hv_deadline_tsc = -1;
13107 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13108 PIN_BASED_VMX_PREEMPTION_TIMER);
13109}
13110#endif
13111
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013112static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013113{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013114 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013115 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013116}
13117
Kai Huang843e4332015-01-28 10:54:28 +080013118static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13119 struct kvm_memory_slot *slot)
13120{
13121 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13122 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13123}
13124
13125static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13126 struct kvm_memory_slot *slot)
13127{
13128 kvm_mmu_slot_set_dirty(kvm, slot);
13129}
13130
13131static void vmx_flush_log_dirty(struct kvm *kvm)
13132{
13133 kvm_flush_pml_buffers(kvm);
13134}
13135
Bandan Dasc5f983f2017-05-05 15:25:14 -040013136static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13137{
13138 struct vmcs12 *vmcs12;
13139 struct vcpu_vmx *vmx = to_vmx(vcpu);
13140 gpa_t gpa;
13141 struct page *page = NULL;
13142 u64 *pml_address;
13143
13144 if (is_guest_mode(vcpu)) {
13145 WARN_ON_ONCE(vmx->nested.pml_full);
13146
13147 /*
13148 * Check if PML is enabled for the nested guest.
13149 * Whether eptp bit 6 is set is already checked
13150 * as part of A/D emulation.
13151 */
13152 vmcs12 = get_vmcs12(vcpu);
13153 if (!nested_cpu_has_pml(vmcs12))
13154 return 0;
13155
Dan Carpenter47698862017-05-10 22:43:17 +030013156 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013157 vmx->nested.pml_full = true;
13158 return 1;
13159 }
13160
13161 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13162
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013163 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13164 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013165 return 0;
13166
13167 pml_address = kmap(page);
13168 pml_address[vmcs12->guest_pml_index--] = gpa;
13169 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013170 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013171 }
13172
13173 return 0;
13174}
13175
Kai Huang843e4332015-01-28 10:54:28 +080013176static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13177 struct kvm_memory_slot *memslot,
13178 gfn_t offset, unsigned long mask)
13179{
13180 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13181}
13182
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013183static void __pi_post_block(struct kvm_vcpu *vcpu)
13184{
13185 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13186 struct pi_desc old, new;
13187 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013188
13189 do {
13190 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013191 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13192 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013193
13194 dest = cpu_physical_id(vcpu->cpu);
13195
13196 if (x2apic_enabled())
13197 new.ndst = dest;
13198 else
13199 new.ndst = (dest << 8) & 0xFF00;
13200
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013201 /* set 'NV' to 'notification vector' */
13202 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013203 } while (cmpxchg64(&pi_desc->control, old.control,
13204 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013205
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013206 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13207 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013208 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013209 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013210 vcpu->pre_pcpu = -1;
13211 }
13212}
13213
Feng Wuefc64402015-09-18 22:29:51 +080013214/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013215 * This routine does the following things for vCPU which is going
13216 * to be blocked if VT-d PI is enabled.
13217 * - Store the vCPU to the wakeup list, so when interrupts happen
13218 * we can find the right vCPU to wake up.
13219 * - Change the Posted-interrupt descriptor as below:
13220 * 'NDST' <-- vcpu->pre_pcpu
13221 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13222 * - If 'ON' is set during this process, which means at least one
13223 * interrupt is posted for this vCPU, we cannot block it, in
13224 * this case, return 1, otherwise, return 0.
13225 *
13226 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013227static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013228{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013229 unsigned int dest;
13230 struct pi_desc old, new;
13231 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13232
13233 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013234 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13235 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013236 return 0;
13237
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013238 WARN_ON(irqs_disabled());
13239 local_irq_disable();
13240 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13241 vcpu->pre_pcpu = vcpu->cpu;
13242 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13243 list_add_tail(&vcpu->blocked_vcpu_list,
13244 &per_cpu(blocked_vcpu_on_cpu,
13245 vcpu->pre_pcpu));
13246 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13247 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013248
13249 do {
13250 old.control = new.control = pi_desc->control;
13251
Feng Wubf9f6ac2015-09-18 22:29:55 +080013252 WARN((pi_desc->sn == 1),
13253 "Warning: SN field of posted-interrupts "
13254 "is set before blocking\n");
13255
13256 /*
13257 * Since vCPU can be preempted during this process,
13258 * vcpu->cpu could be different with pre_pcpu, we
13259 * need to set pre_pcpu as the destination of wakeup
13260 * notification event, then we can find the right vCPU
13261 * to wakeup in wakeup handler if interrupts happen
13262 * when the vCPU is in blocked state.
13263 */
13264 dest = cpu_physical_id(vcpu->pre_pcpu);
13265
13266 if (x2apic_enabled())
13267 new.ndst = dest;
13268 else
13269 new.ndst = (dest << 8) & 0xFF00;
13270
13271 /* set 'NV' to 'wakeup vector' */
13272 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013273 } while (cmpxchg64(&pi_desc->control, old.control,
13274 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013275
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013276 /* We should not block the vCPU if an interrupt is posted for it. */
13277 if (pi_test_on(pi_desc) == 1)
13278 __pi_post_block(vcpu);
13279
13280 local_irq_enable();
13281 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013282}
13283
Yunhong Jiangbc225122016-06-13 14:19:58 -070013284static int vmx_pre_block(struct kvm_vcpu *vcpu)
13285{
13286 if (pi_pre_block(vcpu))
13287 return 1;
13288
Yunhong Jiang64672c92016-06-13 14:19:59 -070013289 if (kvm_lapic_hv_timer_in_use(vcpu))
13290 kvm_lapic_switch_to_sw_timer(vcpu);
13291
Yunhong Jiangbc225122016-06-13 14:19:58 -070013292 return 0;
13293}
13294
13295static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013296{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013297 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013298 return;
13299
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013300 WARN_ON(irqs_disabled());
13301 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013302 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013303 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013304}
13305
Yunhong Jiangbc225122016-06-13 14:19:58 -070013306static void vmx_post_block(struct kvm_vcpu *vcpu)
13307{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013308 if (kvm_x86_ops->set_hv_timer)
13309 kvm_lapic_switch_to_hv_timer(vcpu);
13310
Yunhong Jiangbc225122016-06-13 14:19:58 -070013311 pi_post_block(vcpu);
13312}
13313
Feng Wubf9f6ac2015-09-18 22:29:55 +080013314/*
Feng Wuefc64402015-09-18 22:29:51 +080013315 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13316 *
13317 * @kvm: kvm
13318 * @host_irq: host irq of the interrupt
13319 * @guest_irq: gsi of the interrupt
13320 * @set: set or unset PI
13321 * returns 0 on success, < 0 on failure
13322 */
13323static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13324 uint32_t guest_irq, bool set)
13325{
13326 struct kvm_kernel_irq_routing_entry *e;
13327 struct kvm_irq_routing_table *irq_rt;
13328 struct kvm_lapic_irq irq;
13329 struct kvm_vcpu *vcpu;
13330 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013331 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013332
13333 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013334 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13335 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013336 return 0;
13337
13338 idx = srcu_read_lock(&kvm->irq_srcu);
13339 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013340 if (guest_irq >= irq_rt->nr_rt_entries ||
13341 hlist_empty(&irq_rt->map[guest_irq])) {
13342 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13343 guest_irq, irq_rt->nr_rt_entries);
13344 goto out;
13345 }
Feng Wuefc64402015-09-18 22:29:51 +080013346
13347 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13348 if (e->type != KVM_IRQ_ROUTING_MSI)
13349 continue;
13350 /*
13351 * VT-d PI cannot support posting multicast/broadcast
13352 * interrupts to a vCPU, we still use interrupt remapping
13353 * for these kind of interrupts.
13354 *
13355 * For lowest-priority interrupts, we only support
13356 * those with single CPU as the destination, e.g. user
13357 * configures the interrupts via /proc/irq or uses
13358 * irqbalance to make the interrupts single-CPU.
13359 *
13360 * We will support full lowest-priority interrupt later.
13361 */
13362
Radim Krčmář371313132016-07-12 22:09:27 +020013363 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013364 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13365 /*
13366 * Make sure the IRTE is in remapped mode if
13367 * we don't handle it in posted mode.
13368 */
13369 ret = irq_set_vcpu_affinity(host_irq, NULL);
13370 if (ret < 0) {
13371 printk(KERN_INFO
13372 "failed to back to remapped mode, irq: %u\n",
13373 host_irq);
13374 goto out;
13375 }
13376
Feng Wuefc64402015-09-18 22:29:51 +080013377 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013378 }
Feng Wuefc64402015-09-18 22:29:51 +080013379
13380 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13381 vcpu_info.vector = irq.vector;
13382
hu huajun2698d822018-04-11 15:16:40 +080013383 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013384 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13385
13386 if (set)
13387 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013388 else
Feng Wuefc64402015-09-18 22:29:51 +080013389 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013390
13391 if (ret < 0) {
13392 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13393 __func__);
13394 goto out;
13395 }
13396 }
13397
13398 ret = 0;
13399out:
13400 srcu_read_unlock(&kvm->irq_srcu, idx);
13401 return ret;
13402}
13403
Ashok Rajc45dcc72016-06-22 14:59:56 +080013404static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13405{
13406 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13407 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13408 FEATURE_CONTROL_LMCE;
13409 else
13410 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13411 ~FEATURE_CONTROL_LMCE;
13412}
13413
Ladi Prosek72d7b372017-10-11 16:54:41 +020013414static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13415{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013416 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13417 if (to_vmx(vcpu)->nested.nested_run_pending)
13418 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013419 return 1;
13420}
13421
Ladi Prosek0234bf82017-10-11 16:54:40 +020013422static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13423{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013424 struct vcpu_vmx *vmx = to_vmx(vcpu);
13425
13426 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13427 if (vmx->nested.smm.guest_mode)
13428 nested_vmx_vmexit(vcpu, -1, 0, 0);
13429
13430 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13431 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013432 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013433 return 0;
13434}
13435
13436static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13437{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013438 struct vcpu_vmx *vmx = to_vmx(vcpu);
13439 int ret;
13440
13441 if (vmx->nested.smm.vmxon) {
13442 vmx->nested.vmxon = true;
13443 vmx->nested.smm.vmxon = false;
13444 }
13445
13446 if (vmx->nested.smm.guest_mode) {
13447 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013448 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013449 vcpu->arch.hflags |= HF_SMM_MASK;
13450 if (ret)
13451 return ret;
13452
13453 vmx->nested.smm.guest_mode = false;
13454 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013455 return 0;
13456}
13457
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013458static int enable_smi_window(struct kvm_vcpu *vcpu)
13459{
13460 return 0;
13461}
13462
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013463static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13464 struct kvm_nested_state __user *user_kvm_nested_state,
13465 u32 user_data_size)
13466{
13467 struct vcpu_vmx *vmx;
13468 struct vmcs12 *vmcs12;
13469 struct kvm_nested_state kvm_state = {
13470 .flags = 0,
13471 .format = 0,
13472 .size = sizeof(kvm_state),
13473 .vmx.vmxon_pa = -1ull,
13474 .vmx.vmcs_pa = -1ull,
13475 };
13476
13477 if (!vcpu)
13478 return kvm_state.size + 2 * VMCS12_SIZE;
13479
13480 vmx = to_vmx(vcpu);
13481 vmcs12 = get_vmcs12(vcpu);
13482 if (nested_vmx_allowed(vcpu) &&
13483 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13484 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13485 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13486
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013487 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013488 kvm_state.size += VMCS12_SIZE;
13489
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013490 if (is_guest_mode(vcpu) &&
13491 nested_cpu_has_shadow_vmcs(vmcs12) &&
13492 vmcs12->vmcs_link_pointer != -1ull)
13493 kvm_state.size += VMCS12_SIZE;
13494 }
13495
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013496 if (vmx->nested.smm.vmxon)
13497 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13498
13499 if (vmx->nested.smm.guest_mode)
13500 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13501
13502 if (is_guest_mode(vcpu)) {
13503 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13504
13505 if (vmx->nested.nested_run_pending)
13506 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13507 }
13508 }
13509
13510 if (user_data_size < kvm_state.size)
13511 goto out;
13512
13513 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13514 return -EFAULT;
13515
13516 if (vmx->nested.current_vmptr == -1ull)
13517 goto out;
13518
13519 /*
13520 * When running L2, the authoritative vmcs12 state is in the
13521 * vmcs02. When running L1, the authoritative vmcs12 state is
13522 * in the shadow vmcs linked to vmcs01, unless
13523 * sync_shadow_vmcs is set, in which case, the authoritative
13524 * vmcs12 state is in the vmcs12 already.
13525 */
13526 if (is_guest_mode(vcpu))
13527 sync_vmcs12(vcpu, vmcs12);
13528 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13529 copy_shadow_to_vmcs12(vmx);
13530
13531 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13532 return -EFAULT;
13533
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013534 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13535 vmcs12->vmcs_link_pointer != -1ull) {
13536 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13537 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13538 return -EFAULT;
13539 }
13540
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013541out:
13542 return kvm_state.size;
13543}
13544
13545static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13546 struct kvm_nested_state __user *user_kvm_nested_state,
13547 struct kvm_nested_state *kvm_state)
13548{
13549 struct vcpu_vmx *vmx = to_vmx(vcpu);
13550 struct vmcs12 *vmcs12;
13551 u32 exit_qual;
13552 int ret;
13553
13554 if (kvm_state->format != 0)
13555 return -EINVAL;
13556
13557 if (!nested_vmx_allowed(vcpu))
13558 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13559
13560 if (kvm_state->vmx.vmxon_pa == -1ull) {
13561 if (kvm_state->vmx.smm.flags)
13562 return -EINVAL;
13563
13564 if (kvm_state->vmx.vmcs_pa != -1ull)
13565 return -EINVAL;
13566
13567 vmx_leave_nested(vcpu);
13568 return 0;
13569 }
13570
13571 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13572 return -EINVAL;
13573
13574 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13575 return -EINVAL;
13576
13577 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13578 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13579 return -EINVAL;
13580
13581 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13582 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13583 return -EINVAL;
13584
13585 if (kvm_state->vmx.smm.flags &
13586 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13587 return -EINVAL;
13588
13589 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13590 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13591 return -EINVAL;
13592
13593 vmx_leave_nested(vcpu);
13594 if (kvm_state->vmx.vmxon_pa == -1ull)
13595 return 0;
13596
13597 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13598 ret = enter_vmx_operation(vcpu);
13599 if (ret)
13600 return ret;
13601
13602 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13603
13604 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13605 vmx->nested.smm.vmxon = true;
13606 vmx->nested.vmxon = false;
13607
13608 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13609 vmx->nested.smm.guest_mode = true;
13610 }
13611
13612 vmcs12 = get_vmcs12(vcpu);
13613 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13614 return -EFAULT;
13615
Liran Alon392b2f22018-06-23 02:35:01 +030013616 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013617 return -EINVAL;
13618
13619 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13620 return 0;
13621
13622 vmx->nested.nested_run_pending =
13623 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13624
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013625 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13626 vmcs12->vmcs_link_pointer != -1ull) {
13627 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13628 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13629 return -EINVAL;
13630
13631 if (copy_from_user(shadow_vmcs12,
13632 user_kvm_nested_state->data + VMCS12_SIZE,
13633 sizeof(*vmcs12)))
13634 return -EFAULT;
13635
13636 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13637 !shadow_vmcs12->hdr.shadow_vmcs)
13638 return -EINVAL;
13639 }
13640
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013641 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13642 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13643 return -EINVAL;
13644
13645 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13646 vmx->nested.nested_run_pending = 1;
13647
13648 vmx->nested.dirty_vmcs12 = true;
13649 ret = enter_vmx_non_root_mode(vcpu, NULL);
13650 if (ret)
13651 return -EINVAL;
13652
13653 return 0;
13654}
13655
Kees Cook404f6aa2016-08-08 16:29:06 -070013656static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013657 .cpu_has_kvm_support = cpu_has_kvm_support,
13658 .disabled_by_bios = vmx_disabled_by_bios,
13659 .hardware_setup = hardware_setup,
13660 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013661 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013662 .hardware_enable = hardware_enable,
13663 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013664 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013665 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013666
Wanpeng Lib31c1142018-03-12 04:53:04 -070013667 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013668 .vm_alloc = vmx_vm_alloc,
13669 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013670
Avi Kivity6aa8b732006-12-10 02:21:36 -080013671 .vcpu_create = vmx_create_vcpu,
13672 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013673 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013674
Avi Kivity04d2cc72007-09-10 18:10:54 +030013675 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013676 .vcpu_load = vmx_vcpu_load,
13677 .vcpu_put = vmx_vcpu_put,
13678
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013679 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013680 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013681 .get_msr = vmx_get_msr,
13682 .set_msr = vmx_set_msr,
13683 .get_segment_base = vmx_get_segment_base,
13684 .get_segment = vmx_get_segment,
13685 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013686 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013687 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013688 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013689 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013690 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013691 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013692 .set_cr3 = vmx_set_cr3,
13693 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013694 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013695 .get_idt = vmx_get_idt,
13696 .set_idt = vmx_set_idt,
13697 .get_gdt = vmx_get_gdt,
13698 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013699 .get_dr6 = vmx_get_dr6,
13700 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013701 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013702 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013703 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013704 .get_rflags = vmx_get_rflags,
13705 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013706
Avi Kivity6aa8b732006-12-10 02:21:36 -080013707 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070013708 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013709
Avi Kivity6aa8b732006-12-10 02:21:36 -080013710 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013711 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013712 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013713 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13714 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013715 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013716 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013717 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013718 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013719 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013720 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013721 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013722 .get_nmi_mask = vmx_get_nmi_mask,
13723 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013724 .enable_nmi_window = enable_nmi_window,
13725 .enable_irq_window = enable_irq_window,
13726 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013727 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013728 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013729 .get_enable_apicv = vmx_get_enable_apicv,
13730 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013731 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013732 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013733 .hwapic_irr_update = vmx_hwapic_irr_update,
13734 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013735 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13736 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013737
Izik Eiduscbc94022007-10-25 00:29:55 +020013738 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013739 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013740 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013741 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013742
Avi Kivity586f9602010-11-18 13:09:54 +020013743 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013744
Sheng Yang17cc3932010-01-05 19:02:27 +080013745 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013746
13747 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013748
13749 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013750 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013751
13752 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013753
13754 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013755
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013756 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013757 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013758
13759 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013760
13761 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013762 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013763 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013764 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013765 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013766
13767 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013768
13769 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013770
13771 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13772 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13773 .flush_log_dirty = vmx_flush_log_dirty,
13774 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013775 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013776
Feng Wubf9f6ac2015-09-18 22:29:55 +080013777 .pre_block = vmx_pre_block,
13778 .post_block = vmx_post_block,
13779
Wei Huang25462f72015-06-19 15:45:05 +020013780 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013781
13782 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013783
13784#ifdef CONFIG_X86_64
13785 .set_hv_timer = vmx_set_hv_timer,
13786 .cancel_hv_timer = vmx_cancel_hv_timer,
13787#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013788
13789 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013790
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013791 .get_nested_state = vmx_get_nested_state,
13792 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013793 .get_vmcs12_pages = nested_get_vmcs12_pages,
13794
Ladi Prosek72d7b372017-10-11 16:54:41 +020013795 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013796 .pre_enter_smm = vmx_pre_enter_smm,
13797 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013798 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013799};
13800
13801static int __init vmx_init(void)
13802{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013803 int r;
13804
13805#if IS_ENABLED(CONFIG_HYPERV)
13806 /*
13807 * Enlightened VMCS usage should be recommended and the host needs
13808 * to support eVMCS v1 or above. We can also disable eVMCS support
13809 * with module parameter.
13810 */
13811 if (enlightened_vmcs &&
13812 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13813 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13814 KVM_EVMCS_VERSION) {
13815 int cpu;
13816
13817 /* Check that we have assist pages on all online CPUs */
13818 for_each_online_cpu(cpu) {
13819 if (!hv_get_vp_assist_page(cpu)) {
13820 enlightened_vmcs = false;
13821 break;
13822 }
13823 }
13824
13825 if (enlightened_vmcs) {
13826 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13827 static_branch_enable(&enable_evmcs);
13828 }
13829 } else {
13830 enlightened_vmcs = false;
13831 }
13832#endif
13833
13834 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013835 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013836 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013837 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013838
Dave Young2965faa2015-09-09 15:38:55 -070013839#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013840 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13841 crash_vmclear_local_loaded_vmcss);
13842#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013843 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013844
He, Qingfdef3ad2007-04-30 09:45:24 +030013845 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013846}
13847
13848static void __exit vmx_exit(void)
13849{
Dave Young2965faa2015-09-09 15:38:55 -070013850#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013851 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013852 synchronize_rcu();
13853#endif
13854
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013855 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013856
13857#if IS_ENABLED(CONFIG_HYPERV)
13858 if (static_branch_unlikely(&enable_evmcs)) {
13859 int cpu;
13860 struct hv_vp_assist_page *vp_ap;
13861 /*
13862 * Reset everything to support using non-enlightened VMCS
13863 * access later (e.g. when we reload the module with
13864 * enlightened_vmcs=0)
13865 */
13866 for_each_online_cpu(cpu) {
13867 vp_ap = hv_get_vp_assist_page(cpu);
13868
13869 if (!vp_ap)
13870 continue;
13871
13872 vp_ap->current_nested_vmcs = 0;
13873 vp_ap->enlighten_vmentry = 0;
13874 }
13875
13876 static_branch_disable(&enable_evmcs);
13877 }
13878#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013879}
13880
13881module_init(vmx_init)
13882module_exit(vmx_exit)