blob: 06fd598ee8a6fa6c3ddd2e4b4def4266653ec54b [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();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002665#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002666 unsigned long fs_base, gs_base;
2667 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002668 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002669
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002670 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002671 return;
2672
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002673 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002674 /*
2675 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2676 * allow segment selectors with cpl > 0 or ti == 1.
2677 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002678 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002679 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002680
2681#ifdef CONFIG_X86_64
Sean Christophersone368b872018-07-23 12:32:41 -07002682 savesegment(ds, vmx->host_state.ds_sel);
2683 savesegment(es, vmx->host_state.es_sel);
2684
2685 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002686 if (likely(is_64bit_mm(current->mm))) {
2687 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002688 fs_sel = current->thread.fsindex;
2689 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002690 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002691 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002692 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002693 savesegment(fs, fs_sel);
2694 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002695 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002696 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002697 }
Sean Christophersone368b872018-07-23 12:32:41 -07002698
2699 if (is_long_mode(&vmx->vcpu))
2700 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2701#else
2702 savesegment(fs, fs_sel);
2703 savesegment(gs, gs_sel);
2704 fs_base = segment_base(fs_sel);
2705 gs_base = segment_base(gs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002706#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002707
2708 vmx->host_state.fs_sel = fs_sel;
2709 if (!(fs_sel & 7)) {
2710 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002711 vmx->host_state.fs_reload_needed = 0;
2712 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002713 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002714 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002715 }
Sean Christophersone368b872018-07-23 12:32:41 -07002716 vmx->host_state.gs_sel = gs_sel;
2717 if (!(gs_sel & 7))
2718 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002719 else {
2720 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002721 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002722 }
2723
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002724 vmcs_writel(HOST_FS_BASE, fs_base);
Sean Christophersone368b872018-07-23 12:32:41 -07002725 vmcs_writel(HOST_GS_BASE, gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002726
Avi Kivity26bb0982009-09-07 11:14:12 +03002727 for (i = 0; i < vmx->save_nmsrs; ++i)
2728 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002729 vmx->guest_msrs[i].data,
2730 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002731}
2732
Avi Kivitya9b21b62008-06-24 11:48:49 +03002733static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002734{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002735 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002736 return;
2737
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002738 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002739 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002740#ifdef CONFIG_X86_64
2741 if (is_long_mode(&vmx->vcpu))
2742 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2743#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002744 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002745 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002746#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002747 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002748#else
2749 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002750#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002751 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002752 if (vmx->host_state.fs_reload_needed)
2753 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002754#ifdef CONFIG_X86_64
2755 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2756 loadsegment(ds, vmx->host_state.ds_sel);
2757 loadsegment(es, vmx->host_state.es_sel);
2758 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002759#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002760 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002761#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002762 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002763#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002764 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002765}
2766
Avi Kivitya9b21b62008-06-24 11:48:49 +03002767static void vmx_load_host_state(struct vcpu_vmx *vmx)
2768{
2769 preempt_disable();
2770 __vmx_load_host_state(vmx);
2771 preempt_enable();
2772}
2773
Feng Wu28b835d2015-09-18 22:29:54 +08002774static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2775{
2776 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2777 struct pi_desc old, new;
2778 unsigned int dest;
2779
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002780 /*
2781 * In case of hot-plug or hot-unplug, we may have to undo
2782 * vmx_vcpu_pi_put even if there is no assigned device. And we
2783 * always keep PI.NDST up to date for simplicity: it makes the
2784 * code easier, and CPU migration is not a fast path.
2785 */
2786 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002787 return;
2788
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002789 /*
2790 * First handle the simple case where no cmpxchg is necessary; just
2791 * allow posting non-urgent interrupts.
2792 *
2793 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2794 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2795 * expects the VCPU to be on the blocked_vcpu_list that matches
2796 * PI.NDST.
2797 */
2798 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2799 vcpu->cpu == cpu) {
2800 pi_clear_sn(pi_desc);
2801 return;
2802 }
2803
2804 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002805 do {
2806 old.control = new.control = pi_desc->control;
2807
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002808 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002809
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002810 if (x2apic_enabled())
2811 new.ndst = dest;
2812 else
2813 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002814
Feng Wu28b835d2015-09-18 22:29:54 +08002815 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002816 } while (cmpxchg64(&pi_desc->control, old.control,
2817 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002818}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002819
Peter Feinerc95ba922016-08-17 09:36:47 -07002820static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2821{
2822 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2823 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2824}
2825
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826/*
2827 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2828 * vcpu mutex is already taken.
2829 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002830static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002832 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002833 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002835 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002836 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002837 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002838 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002839
2840 /*
2841 * Read loaded_vmcs->cpu should be before fetching
2842 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2843 * See the comments in __loaded_vmcs_clear().
2844 */
2845 smp_rmb();
2846
Nadav Har'Eld462b812011-05-24 15:26:10 +03002847 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2848 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002849 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002850 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002851 }
2852
2853 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2854 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2855 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002856 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002857 }
2858
2859 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002860 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002861 unsigned long sysenter_esp;
2862
2863 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002864
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 /*
2866 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002867 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002869 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002870 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002871 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002873 /*
2874 * VM exits change the host TR limit to 0x67 after a VM
2875 * exit. This is okay, since 0x67 covers everything except
2876 * the IO bitmap and have have code to handle the IO bitmap
2877 * being lost after a VM exit.
2878 */
2879 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2880
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2882 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002883
Nadav Har'Eld462b812011-05-24 15:26:10 +03002884 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 }
Feng Wu28b835d2015-09-18 22:29:54 +08002886
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002887 /* Setup TSC multiplier */
2888 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002889 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2890 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002891
Feng Wu28b835d2015-09-18 22:29:54 +08002892 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002893 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002894 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002895}
2896
2897static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2898{
2899 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2900
2901 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002902 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2903 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002904 return;
2905
2906 /* Set SN when the vCPU is preempted */
2907 if (vcpu->preempted)
2908 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909}
2910
2911static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2912{
Feng Wu28b835d2015-09-18 22:29:54 +08002913 vmx_vcpu_pi_put(vcpu);
2914
Avi Kivitya9b21b62008-06-24 11:48:49 +03002915 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916}
2917
Wanpeng Lif244dee2017-07-20 01:11:54 -07002918static bool emulation_required(struct kvm_vcpu *vcpu)
2919{
2920 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2921}
2922
Avi Kivityedcafe32009-12-30 18:07:40 +02002923static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2924
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002925/*
2926 * Return the cr0 value that a nested guest would read. This is a combination
2927 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2928 * its hypervisor (cr0_read_shadow).
2929 */
2930static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2931{
2932 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2933 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2934}
2935static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2936{
2937 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2938 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2939}
2940
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2942{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002943 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002944
Avi Kivity6de12732011-03-07 12:51:22 +02002945 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2946 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2947 rflags = vmcs_readl(GUEST_RFLAGS);
2948 if (to_vmx(vcpu)->rmode.vm86_active) {
2949 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2950 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2951 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2952 }
2953 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002954 }
Avi Kivity6de12732011-03-07 12:51:22 +02002955 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956}
2957
2958static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2959{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002960 unsigned long old_rflags = vmx_get_rflags(vcpu);
2961
Avi Kivity6de12732011-03-07 12:51:22 +02002962 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2963 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002964 if (to_vmx(vcpu)->rmode.vm86_active) {
2965 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002966 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002967 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002969
2970 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2971 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972}
2973
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002974static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002975{
2976 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2977 int ret = 0;
2978
2979 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002980 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002981 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002982 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002983
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002984 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002985}
2986
2987static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2988{
2989 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2990 u32 interruptibility = interruptibility_old;
2991
2992 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2993
Jan Kiszka48005f62010-02-19 19:38:07 +01002994 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002995 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002996 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002997 interruptibility |= GUEST_INTR_STATE_STI;
2998
2999 if ((interruptibility != interruptibility_old))
3000 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3001}
3002
Avi Kivity6aa8b732006-12-10 02:21:36 -08003003static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3004{
3005 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003007 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003008 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003009 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010
Glauber Costa2809f5d2009-05-12 16:21:05 -04003011 /* skipping an emulated instruction also counts */
3012 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013}
3014
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003015static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3016 unsigned long exit_qual)
3017{
3018 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3019 unsigned int nr = vcpu->arch.exception.nr;
3020 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3021
3022 if (vcpu->arch.exception.has_error_code) {
3023 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3024 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3025 }
3026
3027 if (kvm_exception_is_soft(nr))
3028 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3029 else
3030 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3031
3032 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3033 vmx_get_nmi_mask(vcpu))
3034 intr_info |= INTR_INFO_UNBLOCK_NMI;
3035
3036 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3037}
3038
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003039/*
3040 * KVM wants to inject page-faults which it got to the guest. This function
3041 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003042 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003043static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003044{
3045 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003046 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003047
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003048 if (nr == PF_VECTOR) {
3049 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003050 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003051 return 1;
3052 }
3053 /*
3054 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3055 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3056 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3057 * can be written only when inject_pending_event runs. This should be
3058 * conditional on a new capability---if the capability is disabled,
3059 * kvm_multiple_exception would write the ancillary information to
3060 * CR2 or DR6, for backwards ABI-compatibility.
3061 */
3062 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3063 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003064 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003065 return 1;
3066 }
3067 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003068 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003069 if (nr == DB_VECTOR)
3070 *exit_qual = vcpu->arch.dr6;
3071 else
3072 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003073 return 1;
3074 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003075 }
3076
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003077 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003078}
3079
Wanpeng Licaa057a2018-03-12 04:53:03 -07003080static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3081{
3082 /*
3083 * Ensure that we clear the HLT state in the VMCS. We don't need to
3084 * explicitly skip the instruction because if the HLT state is set,
3085 * then the instruction is already executing and RIP has already been
3086 * advanced.
3087 */
3088 if (kvm_hlt_in_guest(vcpu->kvm) &&
3089 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3090 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3091}
3092
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003093static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003094{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003095 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003096 unsigned nr = vcpu->arch.exception.nr;
3097 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003098 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003099 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003100
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003101 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003102 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003103 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3104 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003105
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003106 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003107 int inc_eip = 0;
3108 if (kvm_exception_is_soft(nr))
3109 inc_eip = vcpu->arch.event_exit_inst_len;
3110 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003111 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003112 return;
3113 }
3114
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003115 WARN_ON_ONCE(vmx->emulation_required);
3116
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003117 if (kvm_exception_is_soft(nr)) {
3118 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3119 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003120 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3121 } else
3122 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3123
3124 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003125
3126 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003127}
3128
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003129static bool vmx_rdtscp_supported(void)
3130{
3131 return cpu_has_vmx_rdtscp();
3132}
3133
Mao, Junjiead756a12012-07-02 01:18:48 +00003134static bool vmx_invpcid_supported(void)
3135{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003136 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003137}
3138
Avi Kivity6aa8b732006-12-10 02:21:36 -08003139/*
Eddie Donga75beee2007-05-17 18:55:15 +03003140 * Swap MSR entry in host/guest MSR entry array.
3141 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003142static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003143{
Avi Kivity26bb0982009-09-07 11:14:12 +03003144 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003145
3146 tmp = vmx->guest_msrs[to];
3147 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3148 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003149}
3150
3151/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003152 * Set up the vmcs to automatically save and restore system
3153 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3154 * mode, as fiddling with msrs is very expensive.
3155 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003156static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003157{
Avi Kivity26bb0982009-09-07 11:14:12 +03003158 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003159
Eddie Donga75beee2007-05-17 18:55:15 +03003160 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003161#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003162 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003163 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
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_LSTAR);
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++);
3169 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003170 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003171 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003172 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003173 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003174 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003175 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003176 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003177 * if efer.sce is enabled.
3178 */
Brian Gerst8c065852010-07-17 09:03:26 -04003179 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003180 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003181 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003182 }
Eddie Donga75beee2007-05-17 18:55:15 +03003183#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003184 index = __find_msr_index(vmx, MSR_EFER);
3185 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003186 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003187
Avi Kivity26bb0982009-09-07 11:14:12 +03003188 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003189
Yang Zhang8d146952013-01-25 10:18:50 +08003190 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003191 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003192}
3193
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003194static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003196 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003198 if (is_guest_mode(vcpu) &&
3199 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3200 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3201
3202 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203}
3204
3205/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003206 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003207 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003208static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003210 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003211 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003212 * We're here if L1 chose not to trap WRMSR to TSC. According
3213 * to the spec, this should set L1's TSC; The offset that L1
3214 * set for L2 remains unchanged, and still needs to be added
3215 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003216 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003217 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003218 /* recalculate vmcs02.TSC_OFFSET: */
3219 vmcs12 = get_vmcs12(vcpu);
3220 vmcs_write64(TSC_OFFSET, offset +
3221 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3222 vmcs12->tsc_offset : 0));
3223 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003224 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3225 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003226 vmcs_write64(TSC_OFFSET, offset);
3227 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228}
3229
Nadav Har'El801d3422011-05-25 23:02:23 +03003230/*
3231 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3232 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3233 * all guests if the "nested" module option is off, and can also be disabled
3234 * for a single guest by disabling its VMX cpuid bit.
3235 */
3236static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3237{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003238 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003239}
3240
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003242 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3243 * returned for the various VMX controls MSRs when nested VMX is enabled.
3244 * The same values should also be used to verify that vmcs12 control fields are
3245 * valid during nested entry from L1 to L2.
3246 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3247 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3248 * bit in the high half is on if the corresponding bit in the control field
3249 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003250 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003251static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003252{
Paolo Bonzini13893092018-02-26 13:40:09 +01003253 if (!nested) {
3254 memset(msrs, 0, sizeof(*msrs));
3255 return;
3256 }
3257
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003258 /*
3259 * Note that as a general rule, the high half of the MSRs (bits in
3260 * the control fields which may be 1) should be initialized by the
3261 * intersection of the underlying hardware's MSR (i.e., features which
3262 * can be supported) and the list of features we want to expose -
3263 * because they are known to be properly supported in our code.
3264 * Also, usually, the low half of the MSRs (bits which must be 1) can
3265 * be set to 0, meaning that L1 may turn off any of these bits. The
3266 * reason is that if one of these bits is necessary, it will appear
3267 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3268 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003269 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003270 * These rules have exceptions below.
3271 */
3272
3273 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003274 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003275 msrs->pinbased_ctls_low,
3276 msrs->pinbased_ctls_high);
3277 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003278 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003279 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003280 PIN_BASED_EXT_INTR_MASK |
3281 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003282 PIN_BASED_VIRTUAL_NMIS |
3283 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003284 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003285 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003286 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003287
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003288 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003289 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003290 msrs->exit_ctls_low,
3291 msrs->exit_ctls_high);
3292 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003293 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003294
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003295 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003296#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003297 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003298#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003299 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003300 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003301 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003302 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003303 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3304
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003305 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003306 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003307
Jan Kiszka2996fca2014-06-16 13:59:43 +02003308 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003309 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003310
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003311 /* entry controls */
3312 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003313 msrs->entry_ctls_low,
3314 msrs->entry_ctls_high);
3315 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003316 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003317 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003318#ifdef CONFIG_X86_64
3319 VM_ENTRY_IA32E_MODE |
3320#endif
3321 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003322 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003323 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003324 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003325 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003326
Jan Kiszka2996fca2014-06-16 13:59:43 +02003327 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003328 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003329
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003330 /* cpu-based controls */
3331 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003332 msrs->procbased_ctls_low,
3333 msrs->procbased_ctls_high);
3334 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003335 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003336 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003337 CPU_BASED_VIRTUAL_INTR_PENDING |
3338 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003339 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3340 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3341 CPU_BASED_CR3_STORE_EXITING |
3342#ifdef CONFIG_X86_64
3343 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3344#endif
3345 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003346 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3347 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3348 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3349 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003350 /*
3351 * We can allow some features even when not supported by the
3352 * hardware. For example, L1 can specify an MSR bitmap - and we
3353 * can use it to avoid exits to L1 - even when L0 runs L2
3354 * without MSR bitmaps.
3355 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003356 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003357 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003358 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003359
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003360 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003361 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003362 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3363
Paolo Bonzini80154d72017-08-24 13:55:35 +02003364 /*
3365 * secondary cpu-based controls. Do not include those that
3366 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3367 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003368 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003369 msrs->secondary_ctls_low,
3370 msrs->secondary_ctls_high);
3371 msrs->secondary_ctls_low = 0;
3372 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003373 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003374 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003375 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003376 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003377 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003378 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003379 /*
3380 * We can emulate "VMCS shadowing," even if the hardware
3381 * doesn't support it.
3382 */
3383 msrs->secondary_ctls_high |=
3384 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003385
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003386 if (enable_ept) {
3387 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003388 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003389 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003390 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003391 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003392 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003393 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003394 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003395 msrs->ept_caps &= vmx_capability.ept;
3396 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003397 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3398 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003399 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003400 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003401 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003402 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003403 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003404 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003405
Bandan Das27c42a12017-08-03 15:54:42 -04003406 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003407 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003408 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003409 /*
3410 * Advertise EPTP switching unconditionally
3411 * since we emulate it
3412 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003413 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003414 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003415 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003416 }
3417
Paolo Bonzinief697a72016-03-18 16:58:38 +01003418 /*
3419 * Old versions of KVM use the single-context version without
3420 * checking for support, so declare that it is supported even
3421 * though it is treated as global context. The alternative is
3422 * not failing the single-context invvpid, and it is worse.
3423 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003424 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003425 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003426 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003427 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003428 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003429 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003430
Radim Krčmář0790ec12015-03-17 14:02:32 +01003431 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003432 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003433 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3434
Jan Kiszkac18911a2013-03-13 16:06:41 +01003435 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003436 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003437 msrs->misc_low,
3438 msrs->misc_high);
3439 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3440 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003441 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003442 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003443 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003444 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003445
3446 /*
3447 * This MSR reports some information about VMX support. We
3448 * should return information about the VMX we emulate for the
3449 * guest, and the VMCS structure we give it - not about the
3450 * VMX support of the underlying hardware.
3451 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003452 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003453 VMCS12_REVISION |
3454 VMX_BASIC_TRUE_CTLS |
3455 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3456 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3457
3458 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003459 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003460
3461 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003462 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003463 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3464 * We picked the standard core2 setting.
3465 */
3466#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3467#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003468 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3469 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003470
3471 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003472 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3473 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003474
3475 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003476 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003477}
3478
David Matlack38991522016-11-29 18:14:08 -08003479/*
3480 * if fixed0[i] == 1: val[i] must be 1
3481 * if fixed1[i] == 0: val[i] must be 0
3482 */
3483static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3484{
3485 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003486}
3487
3488static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3489{
David Matlack38991522016-11-29 18:14:08 -08003490 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003491}
3492
3493static inline u64 vmx_control_msr(u32 low, u32 high)
3494{
3495 return low | ((u64)high << 32);
3496}
3497
David Matlack62cc6b9d2016-11-29 18:14:07 -08003498static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3499{
3500 superset &= mask;
3501 subset &= mask;
3502
3503 return (superset | subset) == superset;
3504}
3505
3506static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3507{
3508 const u64 feature_and_reserved =
3509 /* feature (except bit 48; see below) */
3510 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3511 /* reserved */
3512 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003513 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003514
3515 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3516 return -EINVAL;
3517
3518 /*
3519 * KVM does not emulate a version of VMX that constrains physical
3520 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3521 */
3522 if (data & BIT_ULL(48))
3523 return -EINVAL;
3524
3525 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3526 vmx_basic_vmcs_revision_id(data))
3527 return -EINVAL;
3528
3529 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3530 return -EINVAL;
3531
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003532 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003533 return 0;
3534}
3535
3536static int
3537vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3538{
3539 u64 supported;
3540 u32 *lowp, *highp;
3541
3542 switch (msr_index) {
3543 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003544 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3545 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003546 break;
3547 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003548 lowp = &vmx->nested.msrs.procbased_ctls_low;
3549 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003550 break;
3551 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003552 lowp = &vmx->nested.msrs.exit_ctls_low;
3553 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003554 break;
3555 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003556 lowp = &vmx->nested.msrs.entry_ctls_low;
3557 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003558 break;
3559 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003560 lowp = &vmx->nested.msrs.secondary_ctls_low;
3561 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003562 break;
3563 default:
3564 BUG();
3565 }
3566
3567 supported = vmx_control_msr(*lowp, *highp);
3568
3569 /* Check must-be-1 bits are still 1. */
3570 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3571 return -EINVAL;
3572
3573 /* Check must-be-0 bits are still 0. */
3574 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3575 return -EINVAL;
3576
3577 *lowp = data;
3578 *highp = data >> 32;
3579 return 0;
3580}
3581
3582static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3583{
3584 const u64 feature_and_reserved_bits =
3585 /* feature */
3586 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3587 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3588 /* reserved */
3589 GENMASK_ULL(13, 9) | BIT_ULL(31);
3590 u64 vmx_misc;
3591
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003592 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3593 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003594
3595 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3596 return -EINVAL;
3597
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003598 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003599 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3600 vmx_misc_preemption_timer_rate(data) !=
3601 vmx_misc_preemption_timer_rate(vmx_misc))
3602 return -EINVAL;
3603
3604 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3605 return -EINVAL;
3606
3607 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3608 return -EINVAL;
3609
3610 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3611 return -EINVAL;
3612
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003613 vmx->nested.msrs.misc_low = data;
3614 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003615
3616 /*
3617 * If L1 has read-only VM-exit information fields, use the
3618 * less permissive vmx_vmwrite_bitmap to specify write
3619 * permissions for the shadow VMCS.
3620 */
3621 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3622 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3623
David Matlack62cc6b9d2016-11-29 18:14:07 -08003624 return 0;
3625}
3626
3627static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3628{
3629 u64 vmx_ept_vpid_cap;
3630
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003631 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3632 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003633
3634 /* Every bit is either reserved or a feature bit. */
3635 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3636 return -EINVAL;
3637
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003638 vmx->nested.msrs.ept_caps = data;
3639 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003640 return 0;
3641}
3642
3643static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3644{
3645 u64 *msr;
3646
3647 switch (msr_index) {
3648 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003650 break;
3651 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003652 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003653 break;
3654 default:
3655 BUG();
3656 }
3657
3658 /*
3659 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3660 * must be 1 in the restored value.
3661 */
3662 if (!is_bitwise_subset(data, *msr, -1ULL))
3663 return -EINVAL;
3664
3665 *msr = data;
3666 return 0;
3667}
3668
3669/*
3670 * Called when userspace is restoring VMX MSRs.
3671 *
3672 * Returns 0 on success, non-0 otherwise.
3673 */
3674static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3675{
3676 struct vcpu_vmx *vmx = to_vmx(vcpu);
3677
Jim Mattsona943ac52018-05-29 09:11:32 -07003678 /*
3679 * Don't allow changes to the VMX capability MSRs while the vCPU
3680 * is in VMX operation.
3681 */
3682 if (vmx->nested.vmxon)
3683 return -EBUSY;
3684
David Matlack62cc6b9d2016-11-29 18:14:07 -08003685 switch (msr_index) {
3686 case MSR_IA32_VMX_BASIC:
3687 return vmx_restore_vmx_basic(vmx, data);
3688 case MSR_IA32_VMX_PINBASED_CTLS:
3689 case MSR_IA32_VMX_PROCBASED_CTLS:
3690 case MSR_IA32_VMX_EXIT_CTLS:
3691 case MSR_IA32_VMX_ENTRY_CTLS:
3692 /*
3693 * The "non-true" VMX capability MSRs are generated from the
3694 * "true" MSRs, so we do not support restoring them directly.
3695 *
3696 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3697 * should restore the "true" MSRs with the must-be-1 bits
3698 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3699 * DEFAULT SETTINGS".
3700 */
3701 return -EINVAL;
3702 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3703 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3704 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3705 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3706 case MSR_IA32_VMX_PROCBASED_CTLS2:
3707 return vmx_restore_control_msr(vmx, msr_index, data);
3708 case MSR_IA32_VMX_MISC:
3709 return vmx_restore_vmx_misc(vmx, data);
3710 case MSR_IA32_VMX_CR0_FIXED0:
3711 case MSR_IA32_VMX_CR4_FIXED0:
3712 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3713 case MSR_IA32_VMX_CR0_FIXED1:
3714 case MSR_IA32_VMX_CR4_FIXED1:
3715 /*
3716 * These MSRs are generated based on the vCPU's CPUID, so we
3717 * do not support restoring them directly.
3718 */
3719 return -EINVAL;
3720 case MSR_IA32_VMX_EPT_VPID_CAP:
3721 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3722 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003723 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003724 return 0;
3725 default:
3726 /*
3727 * The rest of the VMX capability MSRs do not support restore.
3728 */
3729 return -EINVAL;
3730 }
3731}
3732
Jan Kiszkacae50132014-01-04 18:47:22 +01003733/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003734static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003735{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003736 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003737 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003738 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003739 break;
3740 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3741 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003742 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003743 msrs->pinbased_ctls_low,
3744 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003745 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3746 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003747 break;
3748 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3749 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003750 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003751 msrs->procbased_ctls_low,
3752 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003753 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3754 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003755 break;
3756 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3757 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003758 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003759 msrs->exit_ctls_low,
3760 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003761 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3762 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003763 break;
3764 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3765 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003766 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003767 msrs->entry_ctls_low,
3768 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003769 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3770 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003771 break;
3772 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003773 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003774 msrs->misc_low,
3775 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003776 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003777 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003778 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003779 break;
3780 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003781 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003782 break;
3783 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003784 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003785 break;
3786 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003787 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003788 break;
3789 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003790 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003791 break;
3792 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003793 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003794 msrs->secondary_ctls_low,
3795 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003796 break;
3797 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003798 *pdata = msrs->ept_caps |
3799 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003800 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003801 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003802 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003803 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003804 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003805 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003806 }
3807
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003808 return 0;
3809}
3810
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003811static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3812 uint64_t val)
3813{
3814 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3815
3816 return !(val & ~valid_bits);
3817}
3818
Tom Lendacky801e4592018-02-21 13:39:51 -06003819static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3820{
Paolo Bonzini13893092018-02-26 13:40:09 +01003821 switch (msr->index) {
3822 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3823 if (!nested)
3824 return 1;
3825 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3826 default:
3827 return 1;
3828 }
3829
3830 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003831}
3832
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003833/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003834 * Reads an msr value (of 'msr_index') into 'pdata'.
3835 * Returns 0 on success, non-0 otherwise.
3836 * Assumes vcpu_load() was already called.
3837 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003838static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003839{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003840 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003841 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003842
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003843 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003844#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003845 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003846 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847 break;
3848 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003849 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003851 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003852 vmx_load_host_state(vmx);
3853 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003854 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003855#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003856 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003857 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003858 case MSR_IA32_SPEC_CTRL:
3859 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003860 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3861 return 1;
3862
3863 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3864 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003865 case MSR_IA32_ARCH_CAPABILITIES:
3866 if (!msr_info->host_initiated &&
3867 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3868 return 1;
3869 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3870 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003872 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003873 break;
3874 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003875 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876 break;
3877 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003878 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003879 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003880 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003881 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003882 (!msr_info->host_initiated &&
3883 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003884 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003885 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003886 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003887 case MSR_IA32_MCG_EXT_CTL:
3888 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003889 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003890 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003891 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003892 msr_info->data = vcpu->arch.mcg_ext_ctl;
3893 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003894 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003895 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003896 break;
3897 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3898 if (!nested_vmx_allowed(vcpu))
3899 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003900 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3901 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003902 case MSR_IA32_XSS:
3903 if (!vmx_xsaves_supported())
3904 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003905 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003906 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003907 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003908 if (!msr_info->host_initiated &&
3909 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003910 return 1;
3911 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003912 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003913 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003914 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003915 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003916 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003917 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003918 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003919 }
3920
Avi Kivity6aa8b732006-12-10 02:21:36 -08003921 return 0;
3922}
3923
Jan Kiszkacae50132014-01-04 18:47:22 +01003924static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3925
Avi Kivity6aa8b732006-12-10 02:21:36 -08003926/*
3927 * Writes msr value into into the appropriate "register".
3928 * Returns 0 on success, non-0 otherwise.
3929 * Assumes vcpu_load() was already called.
3930 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003931static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003932{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003933 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003934 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003935 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003936 u32 msr_index = msr_info->index;
3937 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003938
Avi Kivity6aa8b732006-12-10 02:21:36 -08003939 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003940 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003941 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003942 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003943#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003944 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003945 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946 vmcs_writel(GUEST_FS_BASE, data);
3947 break;
3948 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003949 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003950 vmcs_writel(GUEST_GS_BASE, data);
3951 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003952 case MSR_KERNEL_GS_BASE:
3953 vmx_load_host_state(vmx);
3954 vmx->msr_guest_kernel_gs_base = data;
3955 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003956#endif
3957 case MSR_IA32_SYSENTER_CS:
3958 vmcs_write32(GUEST_SYSENTER_CS, data);
3959 break;
3960 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003961 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962 break;
3963 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003964 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003965 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003966 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003967 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003968 (!msr_info->host_initiated &&
3969 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003970 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003971 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003972 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003973 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003974 vmcs_write64(GUEST_BNDCFGS, data);
3975 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003976 case MSR_IA32_SPEC_CTRL:
3977 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003978 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3979 return 1;
3980
3981 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003982 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003983 return 1;
3984
3985 vmx->spec_ctrl = data;
3986
3987 if (!data)
3988 break;
3989
3990 /*
3991 * For non-nested:
3992 * When it's written (to non-zero) for the first time, pass
3993 * it through.
3994 *
3995 * For nested:
3996 * The handling of the MSR bitmap for L2 guests is done in
3997 * nested_vmx_merge_msr_bitmap. We should not touch the
3998 * vmcs02.msr_bitmap here since it gets completely overwritten
3999 * in the merging. We update the vmcs01 here for L1 as well
4000 * since it will end up touching the MSR anyway now.
4001 */
4002 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4003 MSR_IA32_SPEC_CTRL,
4004 MSR_TYPE_RW);
4005 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004006 case MSR_IA32_PRED_CMD:
4007 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004008 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4009 return 1;
4010
4011 if (data & ~PRED_CMD_IBPB)
4012 return 1;
4013
4014 if (!data)
4015 break;
4016
4017 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4018
4019 /*
4020 * For non-nested:
4021 * When it's written (to non-zero) for the first time, pass
4022 * it through.
4023 *
4024 * For nested:
4025 * The handling of the MSR bitmap for L2 guests is done in
4026 * nested_vmx_merge_msr_bitmap. We should not touch the
4027 * vmcs02.msr_bitmap here since it gets completely overwritten
4028 * in the merging.
4029 */
4030 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4031 MSR_TYPE_W);
4032 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004033 case MSR_IA32_ARCH_CAPABILITIES:
4034 if (!msr_info->host_initiated)
4035 return 1;
4036 vmx->arch_capabilities = data;
4037 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004038 case MSR_IA32_CR_PAT:
4039 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004040 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4041 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004042 vmcs_write64(GUEST_IA32_PAT, data);
4043 vcpu->arch.pat = data;
4044 break;
4045 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004046 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004047 break;
Will Auldba904632012-11-29 12:42:50 -08004048 case MSR_IA32_TSC_ADJUST:
4049 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004050 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004051 case MSR_IA32_MCG_EXT_CTL:
4052 if ((!msr_info->host_initiated &&
4053 !(to_vmx(vcpu)->msr_ia32_feature_control &
4054 FEATURE_CONTROL_LMCE)) ||
4055 (data & ~MCG_EXT_CTL_LMCE_EN))
4056 return 1;
4057 vcpu->arch.mcg_ext_ctl = data;
4058 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004059 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004060 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004061 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004062 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4063 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004064 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004065 if (msr_info->host_initiated && data == 0)
4066 vmx_leave_nested(vcpu);
4067 break;
4068 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004069 if (!msr_info->host_initiated)
4070 return 1; /* they are read-only */
4071 if (!nested_vmx_allowed(vcpu))
4072 return 1;
4073 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004074 case MSR_IA32_XSS:
4075 if (!vmx_xsaves_supported())
4076 return 1;
4077 /*
4078 * The only supported bit as of Skylake is bit 8, but
4079 * it is not supported on KVM.
4080 */
4081 if (data != 0)
4082 return 1;
4083 vcpu->arch.ia32_xss = data;
4084 if (vcpu->arch.ia32_xss != host_xss)
4085 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4086 vcpu->arch.ia32_xss, host_xss);
4087 else
4088 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4089 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004090 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004091 if (!msr_info->host_initiated &&
4092 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004093 return 1;
4094 /* Check reserved bit, higher 32 bits should be zero */
4095 if ((data >> 32) != 0)
4096 return 1;
4097 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004099 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004100 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004101 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004102 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004103 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4104 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004105 ret = kvm_set_shared_msr(msr->index, msr->data,
4106 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004107 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004108 if (ret)
4109 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004110 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004111 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004112 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004113 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004114 }
4115
Eddie Dong2cc51562007-05-21 07:28:09 +03004116 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004117}
4118
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004119static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004121 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4122 switch (reg) {
4123 case VCPU_REGS_RSP:
4124 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4125 break;
4126 case VCPU_REGS_RIP:
4127 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4128 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004129 case VCPU_EXREG_PDPTR:
4130 if (enable_ept)
4131 ept_save_pdptrs(vcpu);
4132 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004133 default:
4134 break;
4135 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136}
4137
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138static __init int cpu_has_kvm_support(void)
4139{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004140 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141}
4142
4143static __init int vmx_disabled_by_bios(void)
4144{
4145 u64 msr;
4146
4147 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004148 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004149 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004150 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4151 && tboot_enabled())
4152 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004153 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004154 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004155 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004156 && !tboot_enabled()) {
4157 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004158 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004159 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004160 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004161 /* launched w/o TXT and VMX disabled */
4162 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4163 && !tboot_enabled())
4164 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004165 }
4166
4167 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168}
4169
Dongxiao Xu7725b892010-05-11 18:29:38 +08004170static void kvm_cpu_vmxon(u64 addr)
4171{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004172 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004173 intel_pt_handle_vmx(1);
4174
Dongxiao Xu7725b892010-05-11 18:29:38 +08004175 asm volatile (ASM_VMX_VMXON_RAX
4176 : : "a"(&addr), "m"(addr)
4177 : "memory", "cc");
4178}
4179
Radim Krčmář13a34e02014-08-28 15:13:03 +02004180static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181{
4182 int cpu = raw_smp_processor_id();
4183 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004184 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004186 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004187 return -EBUSY;
4188
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004189 /*
4190 * This can happen if we hot-added a CPU but failed to allocate
4191 * VP assist page for it.
4192 */
4193 if (static_branch_unlikely(&enable_evmcs) &&
4194 !hv_get_vp_assist_page(cpu))
4195 return -EFAULT;
4196
Nadav Har'Eld462b812011-05-24 15:26:10 +03004197 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004198 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4199 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004200
4201 /*
4202 * Now we can enable the vmclear operation in kdump
4203 * since the loaded_vmcss_on_cpu list on this cpu
4204 * has been initialized.
4205 *
4206 * Though the cpu is not in VMX operation now, there
4207 * is no problem to enable the vmclear operation
4208 * for the loaded_vmcss_on_cpu list is empty!
4209 */
4210 crash_enable_local_vmclear(cpu);
4211
Avi Kivity6aa8b732006-12-10 02:21:36 -08004212 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004213
4214 test_bits = FEATURE_CONTROL_LOCKED;
4215 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4216 if (tboot_enabled())
4217 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4218
4219 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004220 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004221 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4222 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004223 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004224 if (enable_ept)
4225 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004226
4227 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004228}
4229
Nadav Har'Eld462b812011-05-24 15:26:10 +03004230static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004231{
4232 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004233 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004234
Nadav Har'Eld462b812011-05-24 15:26:10 +03004235 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4236 loaded_vmcss_on_cpu_link)
4237 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004238}
4239
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004240
4241/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4242 * tricks.
4243 */
4244static void kvm_cpu_vmxoff(void)
4245{
4246 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004247
4248 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004249 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004250}
4251
Radim Krčmář13a34e02014-08-28 15:13:03 +02004252static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004254 vmclear_local_loaded_vmcss();
4255 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004256}
4257
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004258static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004259 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004260{
4261 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004262 u32 ctl = ctl_min | ctl_opt;
4263
4264 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4265
4266 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4267 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4268
4269 /* Ensure minimum (required) set of control bits are supported. */
4270 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004271 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004272
4273 *result = ctl;
4274 return 0;
4275}
4276
Avi Kivity110312c2010-12-21 12:54:20 +02004277static __init bool allow_1_setting(u32 msr, u32 ctl)
4278{
4279 u32 vmx_msr_low, vmx_msr_high;
4280
4281 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4282 return vmx_msr_high & ctl;
4283}
4284
Yang, Sheng002c7f72007-07-31 14:23:01 +03004285static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004286{
4287 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004288 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004289 u32 _pin_based_exec_control = 0;
4290 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004291 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004292 u32 _vmexit_control = 0;
4293 u32 _vmentry_control = 0;
4294
Paolo Bonzini13893092018-02-26 13:40:09 +01004295 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304296 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004297#ifdef CONFIG_X86_64
4298 CPU_BASED_CR8_LOAD_EXITING |
4299 CPU_BASED_CR8_STORE_EXITING |
4300#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004301 CPU_BASED_CR3_LOAD_EXITING |
4302 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004303 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004304 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004305 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004306 CPU_BASED_MWAIT_EXITING |
4307 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004308 CPU_BASED_INVLPG_EXITING |
4309 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004310
Sheng Yangf78e0e22007-10-29 09:40:42 +08004311 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004312 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004313 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004314 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4315 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004316 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004317#ifdef CONFIG_X86_64
4318 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4319 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4320 ~CPU_BASED_CR8_STORE_EXITING;
4321#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004322 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004323 min2 = 0;
4324 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004325 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004326 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004327 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004328 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004329 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004330 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004331 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004332 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004333 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004334 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004335 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004336 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004337 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004338 SECONDARY_EXEC_RDSEED_EXITING |
4339 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004340 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004341 SECONDARY_EXEC_TSC_SCALING |
4342 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004343 if (adjust_vmx_controls(min2, opt2,
4344 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004345 &_cpu_based_2nd_exec_control) < 0)
4346 return -EIO;
4347 }
4348#ifndef CONFIG_X86_64
4349 if (!(_cpu_based_2nd_exec_control &
4350 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4351 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4352#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004353
4354 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4355 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004356 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004357 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4358 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004359
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004360 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4361 &vmx_capability.ept, &vmx_capability.vpid);
4362
Sheng Yangd56f5462008-04-25 10:13:16 +08004363 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004364 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4365 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004366 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4367 CPU_BASED_CR3_STORE_EXITING |
4368 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004369 } else if (vmx_capability.ept) {
4370 vmx_capability.ept = 0;
4371 pr_warn_once("EPT CAP should not exist if not support "
4372 "1-setting enable EPT VM-execution control\n");
4373 }
4374 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4375 vmx_capability.vpid) {
4376 vmx_capability.vpid = 0;
4377 pr_warn_once("VPID CAP should not exist if not support "
4378 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004379 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004380
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004381 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004382#ifdef CONFIG_X86_64
4383 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4384#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004385 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004386 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004387 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4388 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004389 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004390
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004391 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4392 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4393 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004394 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4395 &_pin_based_exec_control) < 0)
4396 return -EIO;
4397
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004398 if (cpu_has_broken_vmx_preemption_timer())
4399 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004400 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004401 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004402 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4403
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004404 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004405 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004406 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4407 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004408 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004410 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004411
4412 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4413 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004414 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004415
4416#ifdef CONFIG_X86_64
4417 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4418 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004419 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004420#endif
4421
4422 /* Require Write-Back (WB) memory type for VMCS accesses. */
4423 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004424 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004425
Yang, Sheng002c7f72007-07-31 14:23:01 +03004426 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004427 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004428 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004429
Liran Alon2307af12018-06-29 22:59:04 +03004430 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004431
Yang, Sheng002c7f72007-07-31 14:23:01 +03004432 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4433 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004434 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004435 vmcs_conf->vmexit_ctrl = _vmexit_control;
4436 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004437
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004438 if (static_branch_unlikely(&enable_evmcs))
4439 evmcs_sanitize_exec_ctrls(vmcs_conf);
4440
Avi Kivity110312c2010-12-21 12:54:20 +02004441 cpu_has_load_ia32_efer =
4442 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4443 VM_ENTRY_LOAD_IA32_EFER)
4444 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4445 VM_EXIT_LOAD_IA32_EFER);
4446
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004447 cpu_has_load_perf_global_ctrl =
4448 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4449 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4450 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4451 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4452
4453 /*
4454 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004455 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004456 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4457 *
4458 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4459 *
4460 * AAK155 (model 26)
4461 * AAP115 (model 30)
4462 * AAT100 (model 37)
4463 * BC86,AAY89,BD102 (model 44)
4464 * BA97 (model 46)
4465 *
4466 */
4467 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4468 switch (boot_cpu_data.x86_model) {
4469 case 26:
4470 case 30:
4471 case 37:
4472 case 44:
4473 case 46:
4474 cpu_has_load_perf_global_ctrl = false;
4475 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4476 "does not work properly. Using workaround\n");
4477 break;
4478 default:
4479 break;
4480 }
4481 }
4482
Borislav Petkov782511b2016-04-04 22:25:03 +02004483 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004484 rdmsrl(MSR_IA32_XSS, host_xss);
4485
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004486 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004487}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004488
Liran Alon491a6032018-06-23 02:35:12 +03004489static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004490{
4491 int node = cpu_to_node(cpu);
4492 struct page *pages;
4493 struct vmcs *vmcs;
4494
Vlastimil Babka96db8002015-09-08 15:03:50 -07004495 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004496 if (!pages)
4497 return NULL;
4498 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004499 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004500
4501 /* KVM supports Enlightened VMCS v1 only */
4502 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004503 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004504 else
Liran Alon392b2f22018-06-23 02:35:01 +03004505 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004506
Liran Alon491a6032018-06-23 02:35:12 +03004507 if (shadow)
4508 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004509 return vmcs;
4510}
4511
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512static void free_vmcs(struct vmcs *vmcs)
4513{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004514 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004515}
4516
Nadav Har'Eld462b812011-05-24 15:26:10 +03004517/*
4518 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4519 */
4520static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4521{
4522 if (!loaded_vmcs->vmcs)
4523 return;
4524 loaded_vmcs_clear(loaded_vmcs);
4525 free_vmcs(loaded_vmcs->vmcs);
4526 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004527 if (loaded_vmcs->msr_bitmap)
4528 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004529 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004530}
4531
Liran Alon491a6032018-06-23 02:35:12 +03004532static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004533{
Liran Alon491a6032018-06-23 02:35:12 +03004534 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004535}
4536
4537static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4538{
Liran Alon491a6032018-06-23 02:35:12 +03004539 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004540 if (!loaded_vmcs->vmcs)
4541 return -ENOMEM;
4542
4543 loaded_vmcs->shadow_vmcs = NULL;
4544 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004545
4546 if (cpu_has_vmx_msr_bitmap()) {
4547 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4548 if (!loaded_vmcs->msr_bitmap)
4549 goto out_vmcs;
4550 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004551
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004552 if (IS_ENABLED(CONFIG_HYPERV) &&
4553 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004554 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4555 struct hv_enlightened_vmcs *evmcs =
4556 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4557
4558 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4559 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004560 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004561 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004562
4563out_vmcs:
4564 free_loaded_vmcs(loaded_vmcs);
4565 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004566}
4567
Sam Ravnborg39959582007-06-01 00:47:13 -07004568static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004569{
4570 int cpu;
4571
Zachary Amsden3230bb42009-09-29 11:38:37 -10004572 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004573 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004574 per_cpu(vmxarea, cpu) = NULL;
4575 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004576}
4577
Jim Mattsond37f4262017-12-22 12:12:16 -08004578enum vmcs_field_width {
4579 VMCS_FIELD_WIDTH_U16 = 0,
4580 VMCS_FIELD_WIDTH_U64 = 1,
4581 VMCS_FIELD_WIDTH_U32 = 2,
4582 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004583};
4584
Jim Mattsond37f4262017-12-22 12:12:16 -08004585static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004586{
4587 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004588 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004589 return (field >> 13) & 0x3 ;
4590}
4591
4592static inline int vmcs_field_readonly(unsigned long field)
4593{
4594 return (((field >> 10) & 0x3) == 1);
4595}
4596
Bandan Dasfe2b2012014-04-21 15:20:14 -04004597static void init_vmcs_shadow_fields(void)
4598{
4599 int i, j;
4600
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004601 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4602 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004603 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004604 (i + 1 == max_shadow_read_only_fields ||
4605 shadow_read_only_fields[i + 1] != field + 1))
4606 pr_err("Missing field from shadow_read_only_field %x\n",
4607 field + 1);
4608
4609 clear_bit(field, vmx_vmread_bitmap);
4610#ifdef CONFIG_X86_64
4611 if (field & 1)
4612 continue;
4613#endif
4614 if (j < i)
4615 shadow_read_only_fields[j] = field;
4616 j++;
4617 }
4618 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004619
4620 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004621 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004622 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004623 (i + 1 == max_shadow_read_write_fields ||
4624 shadow_read_write_fields[i + 1] != field + 1))
4625 pr_err("Missing field from shadow_read_write_field %x\n",
4626 field + 1);
4627
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004628 /*
4629 * PML and the preemption timer can be emulated, but the
4630 * processor cannot vmwrite to fields that don't exist
4631 * on bare metal.
4632 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004633 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004634 case GUEST_PML_INDEX:
4635 if (!cpu_has_vmx_pml())
4636 continue;
4637 break;
4638 case VMX_PREEMPTION_TIMER_VALUE:
4639 if (!cpu_has_vmx_preemption_timer())
4640 continue;
4641 break;
4642 case GUEST_INTR_STATUS:
4643 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004644 continue;
4645 break;
4646 default:
4647 break;
4648 }
4649
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004650 clear_bit(field, vmx_vmwrite_bitmap);
4651 clear_bit(field, vmx_vmread_bitmap);
4652#ifdef CONFIG_X86_64
4653 if (field & 1)
4654 continue;
4655#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004656 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004657 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004658 j++;
4659 }
4660 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004661}
4662
Avi Kivity6aa8b732006-12-10 02:21:36 -08004663static __init int alloc_kvm_area(void)
4664{
4665 int cpu;
4666
Zachary Amsden3230bb42009-09-29 11:38:37 -10004667 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004668 struct vmcs *vmcs;
4669
Liran Alon491a6032018-06-23 02:35:12 +03004670 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004671 if (!vmcs) {
4672 free_kvm_area();
4673 return -ENOMEM;
4674 }
4675
Liran Alon2307af12018-06-29 22:59:04 +03004676 /*
4677 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4678 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4679 * revision_id reported by MSR_IA32_VMX_BASIC.
4680 *
4681 * However, even though not explictly documented by
4682 * TLFS, VMXArea passed as VMXON argument should
4683 * still be marked with revision_id reported by
4684 * physical CPU.
4685 */
4686 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004687 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004688
Avi Kivity6aa8b732006-12-10 02:21:36 -08004689 per_cpu(vmxarea, cpu) = vmcs;
4690 }
4691 return 0;
4692}
4693
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004694static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004695 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004696{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004697 if (!emulate_invalid_guest_state) {
4698 /*
4699 * CS and SS RPL should be equal during guest entry according
4700 * to VMX spec, but in reality it is not always so. Since vcpu
4701 * is in the middle of the transition from real mode to
4702 * protected mode it is safe to assume that RPL 0 is a good
4703 * default value.
4704 */
4705 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004706 save->selector &= ~SEGMENT_RPL_MASK;
4707 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004708 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004709 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004710 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004711}
4712
4713static void enter_pmode(struct kvm_vcpu *vcpu)
4714{
4715 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004716 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004717
Gleb Natapovd99e4152012-12-20 16:57:45 +02004718 /*
4719 * Update real mode segment cache. It may be not up-to-date if sement
4720 * register was written while vcpu was in a guest mode.
4721 */
4722 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4723 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4724 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4725 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4726 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4727 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4728
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004729 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004730
Avi Kivity2fb92db2011-04-27 19:42:18 +03004731 vmx_segment_cache_clear(vmx);
4732
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004733 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004734
4735 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004736 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4737 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004738 vmcs_writel(GUEST_RFLAGS, flags);
4739
Rusty Russell66aee912007-07-17 23:34:16 +10004740 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4741 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742
4743 update_exception_bitmap(vcpu);
4744
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004745 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4746 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4747 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4748 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4749 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4750 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004751}
4752
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004753static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004754{
Mathias Krause772e0312012-08-30 01:30:19 +02004755 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004756 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757
Gleb Natapovd99e4152012-12-20 16:57:45 +02004758 var.dpl = 0x3;
4759 if (seg == VCPU_SREG_CS)
4760 var.type = 0x3;
4761
4762 if (!emulate_invalid_guest_state) {
4763 var.selector = var.base >> 4;
4764 var.base = var.base & 0xffff0;
4765 var.limit = 0xffff;
4766 var.g = 0;
4767 var.db = 0;
4768 var.present = 1;
4769 var.s = 1;
4770 var.l = 0;
4771 var.unusable = 0;
4772 var.type = 0x3;
4773 var.avl = 0;
4774 if (save->base & 0xf)
4775 printk_once(KERN_WARNING "kvm: segment base is not "
4776 "paragraph aligned when entering "
4777 "protected mode (seg=%d)", seg);
4778 }
4779
4780 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004781 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004782 vmcs_write32(sf->limit, var.limit);
4783 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004784}
4785
4786static void enter_rmode(struct kvm_vcpu *vcpu)
4787{
4788 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004789 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004790 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004791
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004792 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4793 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4794 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4795 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4796 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004797 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4798 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004799
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004800 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004801
Gleb Natapov776e58e2011-03-13 12:34:27 +02004802 /*
4803 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004804 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004805 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004806 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004807 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4808 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004809
Avi Kivity2fb92db2011-04-27 19:42:18 +03004810 vmx_segment_cache_clear(vmx);
4811
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004812 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004813 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004814 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4815
4816 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004817 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004818
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004819 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820
4821 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004822 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004823 update_exception_bitmap(vcpu);
4824
Gleb Natapovd99e4152012-12-20 16:57:45 +02004825 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4826 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4827 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4828 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4829 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4830 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004831
Eddie Dong8668a3c2007-10-10 14:26:45 +08004832 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004833}
4834
Amit Shah401d10d2009-02-20 22:53:37 +05304835static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4836{
4837 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004838 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4839
4840 if (!msr)
4841 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304842
Avi Kivity44ea2b12009-09-06 15:55:37 +03004843 /*
4844 * Force kernel_gs_base reloading before EFER changes, as control
4845 * of this msr depends on is_long_mode().
4846 */
4847 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004848 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304849 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004850 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304851 msr->data = efer;
4852 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004853 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304854
4855 msr->data = efer & ~EFER_LME;
4856 }
4857 setup_msrs(vmx);
4858}
4859
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004860#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004861
4862static void enter_lmode(struct kvm_vcpu *vcpu)
4863{
4864 u32 guest_tr_ar;
4865
Avi Kivity2fb92db2011-04-27 19:42:18 +03004866 vmx_segment_cache_clear(to_vmx(vcpu));
4867
Avi Kivity6aa8b732006-12-10 02:21:36 -08004868 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004869 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004870 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4871 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004873 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4874 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004875 }
Avi Kivityda38f432010-07-06 11:30:49 +03004876 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004877}
4878
4879static void exit_lmode(struct kvm_vcpu *vcpu)
4880{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004881 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004882 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883}
4884
4885#endif
4886
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004887static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4888 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004889{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004890 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004891 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4892 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004893 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004894 } else {
4895 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004896 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004897}
4898
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004899static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004900{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004901 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004902}
4903
Junaid Shahidfaff8752018-06-29 13:10:05 -07004904static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
4905{
4906 int vpid = to_vmx(vcpu)->vpid;
4907
4908 if (!vpid_sync_vcpu_addr(vpid, addr))
4909 vpid_sync_context(vpid);
4910
4911 /*
4912 * If VPIDs are not supported or enabled, then the above is a no-op.
4913 * But we don't really need a TLB flush in that case anyway, because
4914 * each VM entry/exit includes an implicit flush when VPID is 0.
4915 */
4916}
4917
Avi Kivitye8467fd2009-12-29 18:43:06 +02004918static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4919{
4920 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4921
4922 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4923 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4924}
4925
Avi Kivityaff48ba2010-12-05 18:56:11 +02004926static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4927{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004928 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004929 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4930 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4931}
4932
Anthony Liguori25c4c272007-04-27 09:29:21 +03004933static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004934{
Avi Kivityfc78f512009-12-07 12:16:48 +02004935 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4936
4937 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4938 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004939}
4940
Sheng Yang14394422008-04-28 12:24:45 +08004941static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4942{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004943 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4944
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004945 if (!test_bit(VCPU_EXREG_PDPTR,
4946 (unsigned long *)&vcpu->arch.regs_dirty))
4947 return;
4948
Sheng Yang14394422008-04-28 12:24:45 +08004949 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004950 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4951 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4952 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4953 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004954 }
4955}
4956
Avi Kivity8f5d5492009-05-31 18:41:29 +03004957static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4958{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004959 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4960
Avi Kivity8f5d5492009-05-31 18:41:29 +03004961 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004962 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4963 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4964 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4965 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004966 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004967
4968 __set_bit(VCPU_EXREG_PDPTR,
4969 (unsigned long *)&vcpu->arch.regs_avail);
4970 __set_bit(VCPU_EXREG_PDPTR,
4971 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004972}
4973
David Matlack38991522016-11-29 18:14:08 -08004974static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4975{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004976 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4977 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004978 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4979
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004980 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004981 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4982 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4983 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4984
4985 return fixed_bits_valid(val, fixed0, fixed1);
4986}
4987
4988static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4989{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004990 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4991 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004992
4993 return fixed_bits_valid(val, fixed0, fixed1);
4994}
4995
4996static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4997{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004998 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4999 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005000
5001 return fixed_bits_valid(val, fixed0, fixed1);
5002}
5003
5004/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5005#define nested_guest_cr4_valid nested_cr4_valid
5006#define nested_host_cr4_valid nested_cr4_valid
5007
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005008static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005009
5010static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5011 unsigned long cr0,
5012 struct kvm_vcpu *vcpu)
5013{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005014 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5015 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005016 if (!(cr0 & X86_CR0_PG)) {
5017 /* From paging/starting to nonpaging */
5018 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005019 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005020 (CPU_BASED_CR3_LOAD_EXITING |
5021 CPU_BASED_CR3_STORE_EXITING));
5022 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005023 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005024 } else if (!is_paging(vcpu)) {
5025 /* From nonpaging to paging */
5026 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005027 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005028 ~(CPU_BASED_CR3_LOAD_EXITING |
5029 CPU_BASED_CR3_STORE_EXITING));
5030 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005031 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005032 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005033
5034 if (!(cr0 & X86_CR0_WP))
5035 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005036}
5037
Avi Kivity6aa8b732006-12-10 02:21:36 -08005038static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5039{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005040 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005041 unsigned long hw_cr0;
5042
Gleb Natapov50378782013-02-04 16:00:28 +02005043 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005044 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005045 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005046 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005047 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005048
Gleb Natapov218e7632013-01-21 15:36:45 +02005049 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5050 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051
Gleb Natapov218e7632013-01-21 15:36:45 +02005052 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5053 enter_rmode(vcpu);
5054 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005055
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005056#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005057 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005058 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005059 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005060 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005061 exit_lmode(vcpu);
5062 }
5063#endif
5064
Sean Christophersonb4d18512018-03-05 12:04:40 -08005065 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005066 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5067
Avi Kivity6aa8b732006-12-10 02:21:36 -08005068 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005069 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005070 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005071
5072 /* depends on vcpu->arch.cr0 to be set to a new value */
5073 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005074}
5075
Yu Zhang855feb62017-08-24 20:27:55 +08005076static int get_ept_level(struct kvm_vcpu *vcpu)
5077{
5078 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5079 return 5;
5080 return 4;
5081}
5082
Peter Feiner995f00a2017-06-30 17:26:32 -07005083static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005084{
Yu Zhang855feb62017-08-24 20:27:55 +08005085 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005086
Yu Zhang855feb62017-08-24 20:27:55 +08005087 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005088
Peter Feiner995f00a2017-06-30 17:26:32 -07005089 if (enable_ept_ad_bits &&
5090 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005091 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005092 eptp |= (root_hpa & PAGE_MASK);
5093
5094 return eptp;
5095}
5096
Avi Kivity6aa8b732006-12-10 02:21:36 -08005097static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5098{
Tianyu Lan877ad952018-07-19 08:40:23 +00005099 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005100 unsigned long guest_cr3;
5101 u64 eptp;
5102
5103 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005104 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005105 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005106 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005107
5108 if (kvm_x86_ops->tlb_remote_flush) {
5109 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5110 to_vmx(vcpu)->ept_pointer = eptp;
5111 to_kvm_vmx(kvm)->ept_pointers_match
5112 = EPT_POINTERS_CHECK;
5113 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5114 }
5115
Sean Christophersone90008d2018-03-05 12:04:37 -08005116 if (enable_unrestricted_guest || is_paging(vcpu) ||
5117 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005118 guest_cr3 = kvm_read_cr3(vcpu);
5119 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005120 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005121 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005122 }
5123
Sheng Yang14394422008-04-28 12:24:45 +08005124 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005125}
5126
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005127static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005128{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005129 /*
5130 * Pass through host's Machine Check Enable value to hw_cr4, which
5131 * is in force while we are in guest mode. Do not let guests control
5132 * this bit, even if host CR4.MCE == 0.
5133 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005134 unsigned long hw_cr4;
5135
5136 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5137 if (enable_unrestricted_guest)
5138 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5139 else if (to_vmx(vcpu)->rmode.vm86_active)
5140 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5141 else
5142 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005143
Sean Christopherson64f7a112018-04-30 10:01:06 -07005144 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5145 if (cr4 & X86_CR4_UMIP) {
5146 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005147 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005148 hw_cr4 &= ~X86_CR4_UMIP;
5149 } else if (!is_guest_mode(vcpu) ||
5150 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5151 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5152 SECONDARY_EXEC_DESC);
5153 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005154
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005155 if (cr4 & X86_CR4_VMXE) {
5156 /*
5157 * To use VMXON (and later other VMX instructions), a guest
5158 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5159 * So basically the check on whether to allow nested VMX
5160 * is here.
5161 */
5162 if (!nested_vmx_allowed(vcpu))
5163 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005164 }
David Matlack38991522016-11-29 18:14:08 -08005165
5166 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005167 return 1;
5168
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005169 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005170
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005171 if (!enable_unrestricted_guest) {
5172 if (enable_ept) {
5173 if (!is_paging(vcpu)) {
5174 hw_cr4 &= ~X86_CR4_PAE;
5175 hw_cr4 |= X86_CR4_PSE;
5176 } else if (!(cr4 & X86_CR4_PAE)) {
5177 hw_cr4 &= ~X86_CR4_PAE;
5178 }
5179 }
5180
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005181 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005182 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5183 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5184 * to be manually disabled when guest switches to non-paging
5185 * mode.
5186 *
5187 * If !enable_unrestricted_guest, the CPU is always running
5188 * with CR0.PG=1 and CR4 needs to be modified.
5189 * If enable_unrestricted_guest, the CPU automatically
5190 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005191 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005192 if (!is_paging(vcpu))
5193 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5194 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005195
Sheng Yang14394422008-04-28 12:24:45 +08005196 vmcs_writel(CR4_READ_SHADOW, cr4);
5197 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005198 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005199}
5200
Avi Kivity6aa8b732006-12-10 02:21:36 -08005201static void vmx_get_segment(struct kvm_vcpu *vcpu,
5202 struct kvm_segment *var, int seg)
5203{
Avi Kivitya9179492011-01-03 14:28:52 +02005204 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005205 u32 ar;
5206
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005207 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005208 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005209 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005210 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005211 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005212 var->base = vmx_read_guest_seg_base(vmx, seg);
5213 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5214 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005215 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005216 var->base = vmx_read_guest_seg_base(vmx, seg);
5217 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5218 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5219 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005220 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005221 var->type = ar & 15;
5222 var->s = (ar >> 4) & 1;
5223 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005224 /*
5225 * Some userspaces do not preserve unusable property. Since usable
5226 * segment has to be present according to VMX spec we can use present
5227 * property to amend userspace bug by making unusable segment always
5228 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5229 * segment as unusable.
5230 */
5231 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005232 var->avl = (ar >> 12) & 1;
5233 var->l = (ar >> 13) & 1;
5234 var->db = (ar >> 14) & 1;
5235 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005236}
5237
Avi Kivitya9179492011-01-03 14:28:52 +02005238static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5239{
Avi Kivitya9179492011-01-03 14:28:52 +02005240 struct kvm_segment s;
5241
5242 if (to_vmx(vcpu)->rmode.vm86_active) {
5243 vmx_get_segment(vcpu, &s, seg);
5244 return s.base;
5245 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005246 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005247}
5248
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005249static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005250{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005251 struct vcpu_vmx *vmx = to_vmx(vcpu);
5252
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005253 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005254 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005255 else {
5256 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005257 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005258 }
Avi Kivity69c73022011-03-07 15:26:44 +02005259}
5260
Avi Kivity653e3102007-05-07 10:55:37 +03005261static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005262{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005263 u32 ar;
5264
Avi Kivityf0495f92012-06-07 17:06:10 +03005265 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005266 ar = 1 << 16;
5267 else {
5268 ar = var->type & 15;
5269 ar |= (var->s & 1) << 4;
5270 ar |= (var->dpl & 3) << 5;
5271 ar |= (var->present & 1) << 7;
5272 ar |= (var->avl & 1) << 12;
5273 ar |= (var->l & 1) << 13;
5274 ar |= (var->db & 1) << 14;
5275 ar |= (var->g & 1) << 15;
5276 }
Avi Kivity653e3102007-05-07 10:55:37 +03005277
5278 return ar;
5279}
5280
5281static void vmx_set_segment(struct kvm_vcpu *vcpu,
5282 struct kvm_segment *var, int seg)
5283{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005284 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005285 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005286
Avi Kivity2fb92db2011-04-27 19:42:18 +03005287 vmx_segment_cache_clear(vmx);
5288
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005289 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5290 vmx->rmode.segs[seg] = *var;
5291 if (seg == VCPU_SREG_TR)
5292 vmcs_write16(sf->selector, var->selector);
5293 else if (var->s)
5294 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005295 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005296 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005297
Avi Kivity653e3102007-05-07 10:55:37 +03005298 vmcs_writel(sf->base, var->base);
5299 vmcs_write32(sf->limit, var->limit);
5300 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005301
5302 /*
5303 * Fix the "Accessed" bit in AR field of segment registers for older
5304 * qemu binaries.
5305 * IA32 arch specifies that at the time of processor reset the
5306 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005307 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005308 * state vmexit when "unrestricted guest" mode is turned on.
5309 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5310 * tree. Newer qemu binaries with that qemu fix would not need this
5311 * kvm hack.
5312 */
5313 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005314 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005315
Gleb Natapovf924d662012-12-12 19:10:55 +02005316 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005317
5318out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005319 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005320}
5321
Avi Kivity6aa8b732006-12-10 02:21:36 -08005322static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5323{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005324 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005325
5326 *db = (ar >> 14) & 1;
5327 *l = (ar >> 13) & 1;
5328}
5329
Gleb Natapov89a27f42010-02-16 10:51:48 +02005330static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005331{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005332 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5333 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005334}
5335
Gleb Natapov89a27f42010-02-16 10:51:48 +02005336static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005337{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005338 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5339 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005340}
5341
Gleb Natapov89a27f42010-02-16 10:51:48 +02005342static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005343{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005344 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5345 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005346}
5347
Gleb Natapov89a27f42010-02-16 10:51:48 +02005348static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005349{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005350 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5351 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005352}
5353
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005354static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5355{
5356 struct kvm_segment var;
5357 u32 ar;
5358
5359 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005360 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005361 if (seg == VCPU_SREG_CS)
5362 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005363 ar = vmx_segment_access_rights(&var);
5364
5365 if (var.base != (var.selector << 4))
5366 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005367 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005368 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005369 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005370 return false;
5371
5372 return true;
5373}
5374
5375static bool code_segment_valid(struct kvm_vcpu *vcpu)
5376{
5377 struct kvm_segment cs;
5378 unsigned int cs_rpl;
5379
5380 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005381 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005382
Avi Kivity1872a3f2009-01-04 23:26:52 +02005383 if (cs.unusable)
5384 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005385 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005386 return false;
5387 if (!cs.s)
5388 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005389 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005390 if (cs.dpl > cs_rpl)
5391 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005392 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005393 if (cs.dpl != cs_rpl)
5394 return false;
5395 }
5396 if (!cs.present)
5397 return false;
5398
5399 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5400 return true;
5401}
5402
5403static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5404{
5405 struct kvm_segment ss;
5406 unsigned int ss_rpl;
5407
5408 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005409 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005410
Avi Kivity1872a3f2009-01-04 23:26:52 +02005411 if (ss.unusable)
5412 return true;
5413 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005414 return false;
5415 if (!ss.s)
5416 return false;
5417 if (ss.dpl != ss_rpl) /* DPL != RPL */
5418 return false;
5419 if (!ss.present)
5420 return false;
5421
5422 return true;
5423}
5424
5425static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5426{
5427 struct kvm_segment var;
5428 unsigned int rpl;
5429
5430 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005431 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005432
Avi Kivity1872a3f2009-01-04 23:26:52 +02005433 if (var.unusable)
5434 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005435 if (!var.s)
5436 return false;
5437 if (!var.present)
5438 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005439 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005440 if (var.dpl < rpl) /* DPL < RPL */
5441 return false;
5442 }
5443
5444 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5445 * rights flags
5446 */
5447 return true;
5448}
5449
5450static bool tr_valid(struct kvm_vcpu *vcpu)
5451{
5452 struct kvm_segment tr;
5453
5454 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5455
Avi Kivity1872a3f2009-01-04 23:26:52 +02005456 if (tr.unusable)
5457 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005458 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005459 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005460 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005461 return false;
5462 if (!tr.present)
5463 return false;
5464
5465 return true;
5466}
5467
5468static bool ldtr_valid(struct kvm_vcpu *vcpu)
5469{
5470 struct kvm_segment ldtr;
5471
5472 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5473
Avi Kivity1872a3f2009-01-04 23:26:52 +02005474 if (ldtr.unusable)
5475 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005476 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005477 return false;
5478 if (ldtr.type != 2)
5479 return false;
5480 if (!ldtr.present)
5481 return false;
5482
5483 return true;
5484}
5485
5486static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5487{
5488 struct kvm_segment cs, ss;
5489
5490 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5491 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5492
Nadav Amitb32a9912015-03-29 16:33:04 +03005493 return ((cs.selector & SEGMENT_RPL_MASK) ==
5494 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005495}
5496
5497/*
5498 * Check if guest state is valid. Returns true if valid, false if
5499 * not.
5500 * We assume that registers are always usable
5501 */
5502static bool guest_state_valid(struct kvm_vcpu *vcpu)
5503{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005504 if (enable_unrestricted_guest)
5505 return true;
5506
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005507 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005508 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005509 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5510 return false;
5511 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5512 return false;
5513 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5514 return false;
5515 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5516 return false;
5517 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5518 return false;
5519 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5520 return false;
5521 } else {
5522 /* protected mode guest state checks */
5523 if (!cs_ss_rpl_check(vcpu))
5524 return false;
5525 if (!code_segment_valid(vcpu))
5526 return false;
5527 if (!stack_segment_valid(vcpu))
5528 return false;
5529 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5530 return false;
5531 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5532 return false;
5533 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5534 return false;
5535 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5536 return false;
5537 if (!tr_valid(vcpu))
5538 return false;
5539 if (!ldtr_valid(vcpu))
5540 return false;
5541 }
5542 /* TODO:
5543 * - Add checks on RIP
5544 * - Add checks on RFLAGS
5545 */
5546
5547 return true;
5548}
5549
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005550static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5551{
5552 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5553}
5554
Mike Dayd77c26f2007-10-08 09:02:08 -04005555static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005556{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005557 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005558 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005559 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005560
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005561 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005562 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005563 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5564 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005565 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005566 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005567 r = kvm_write_guest_page(kvm, fn++, &data,
5568 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005569 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 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5575 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005576 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005577 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005578 r = kvm_write_guest_page(kvm, fn, &data,
5579 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5580 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005581out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005582 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005583 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005584}
5585
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005586static int init_rmode_identity_map(struct kvm *kvm)
5587{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005588 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005589 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005590 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005591 u32 tmp;
5592
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005593 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005594 mutex_lock(&kvm->slots_lock);
5595
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005596 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005597 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005598
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005599 if (!kvm_vmx->ept_identity_map_addr)
5600 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5601 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005602
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005603 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005604 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005605 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005606 goto out2;
5607
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005608 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005609 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5610 if (r < 0)
5611 goto out;
5612 /* Set up identity-mapping pagetable for EPT in real mode */
5613 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5614 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5615 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5616 r = kvm_write_guest_page(kvm, identity_map_pfn,
5617 &tmp, i * sizeof(tmp), sizeof(tmp));
5618 if (r < 0)
5619 goto out;
5620 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005621 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005622
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005623out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005624 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005625
5626out2:
5627 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005628 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005629}
5630
Avi Kivity6aa8b732006-12-10 02:21:36 -08005631static void seg_setup(int seg)
5632{
Mathias Krause772e0312012-08-30 01:30:19 +02005633 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005634 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005635
5636 vmcs_write16(sf->selector, 0);
5637 vmcs_writel(sf->base, 0);
5638 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005639 ar = 0x93;
5640 if (seg == VCPU_SREG_CS)
5641 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005642
5643 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005644}
5645
Sheng Yangf78e0e22007-10-29 09:40:42 +08005646static int alloc_apic_access_page(struct kvm *kvm)
5647{
Xiao Guangrong44841412012-09-07 14:14:20 +08005648 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005649 int r = 0;
5650
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005651 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005652 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005653 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005654 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5655 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005656 if (r)
5657 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005658
Tang Chen73a6d942014-09-11 13:38:00 +08005659 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005660 if (is_error_page(page)) {
5661 r = -EFAULT;
5662 goto out;
5663 }
5664
Tang Chenc24ae0d2014-09-24 15:57:58 +08005665 /*
5666 * Do not pin the page in memory, so that memory hot-unplug
5667 * is able to migrate it.
5668 */
5669 put_page(page);
5670 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005671out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005672 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005673 return r;
5674}
5675
Wanpeng Li991e7a02015-09-16 17:30:05 +08005676static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005677{
5678 int vpid;
5679
Avi Kivity919818a2009-03-23 18:01:29 +02005680 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005681 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005682 spin_lock(&vmx_vpid_lock);
5683 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005684 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005685 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005686 else
5687 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005688 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005689 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005690}
5691
Wanpeng Li991e7a02015-09-16 17:30:05 +08005692static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005693{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005694 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005695 return;
5696 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005697 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005698 spin_unlock(&vmx_vpid_lock);
5699}
5700
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005701static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5702 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005703{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005704 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005705
5706 if (!cpu_has_vmx_msr_bitmap())
5707 return;
5708
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005709 if (static_branch_unlikely(&enable_evmcs))
5710 evmcs_touch_msr_bitmap();
5711
Sheng Yang25c5f222008-03-28 13:18:56 +08005712 /*
5713 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5714 * have the write-low and read-high bitmap offsets the wrong way round.
5715 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5716 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005717 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005718 if (type & MSR_TYPE_R)
5719 /* read-low */
5720 __clear_bit(msr, msr_bitmap + 0x000 / f);
5721
5722 if (type & MSR_TYPE_W)
5723 /* write-low */
5724 __clear_bit(msr, msr_bitmap + 0x800 / f);
5725
Sheng Yang25c5f222008-03-28 13:18:56 +08005726 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5727 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005728 if (type & MSR_TYPE_R)
5729 /* read-high */
5730 __clear_bit(msr, msr_bitmap + 0x400 / f);
5731
5732 if (type & MSR_TYPE_W)
5733 /* write-high */
5734 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5735
5736 }
5737}
5738
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005739static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5740 u32 msr, int type)
5741{
5742 int f = sizeof(unsigned long);
5743
5744 if (!cpu_has_vmx_msr_bitmap())
5745 return;
5746
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005747 if (static_branch_unlikely(&enable_evmcs))
5748 evmcs_touch_msr_bitmap();
5749
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005750 /*
5751 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5752 * have the write-low and read-high bitmap offsets the wrong way round.
5753 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5754 */
5755 if (msr <= 0x1fff) {
5756 if (type & MSR_TYPE_R)
5757 /* read-low */
5758 __set_bit(msr, msr_bitmap + 0x000 / f);
5759
5760 if (type & MSR_TYPE_W)
5761 /* write-low */
5762 __set_bit(msr, msr_bitmap + 0x800 / f);
5763
5764 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5765 msr &= 0x1fff;
5766 if (type & MSR_TYPE_R)
5767 /* read-high */
5768 __set_bit(msr, msr_bitmap + 0x400 / f);
5769
5770 if (type & MSR_TYPE_W)
5771 /* write-high */
5772 __set_bit(msr, msr_bitmap + 0xc00 / f);
5773
5774 }
5775}
5776
5777static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5778 u32 msr, int type, bool value)
5779{
5780 if (value)
5781 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5782 else
5783 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5784}
5785
Wincy Vanf2b93282015-02-03 23:56:03 +08005786/*
5787 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5788 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5789 */
5790static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5791 unsigned long *msr_bitmap_nested,
5792 u32 msr, int type)
5793{
5794 int f = sizeof(unsigned long);
5795
Wincy Vanf2b93282015-02-03 23:56:03 +08005796 /*
5797 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5798 * have the write-low and read-high bitmap offsets the wrong way round.
5799 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5800 */
5801 if (msr <= 0x1fff) {
5802 if (type & MSR_TYPE_R &&
5803 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5804 /* read-low */
5805 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5806
5807 if (type & MSR_TYPE_W &&
5808 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5809 /* write-low */
5810 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5811
5812 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5813 msr &= 0x1fff;
5814 if (type & MSR_TYPE_R &&
5815 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5816 /* read-high */
5817 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5818
5819 if (type & MSR_TYPE_W &&
5820 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5821 /* write-high */
5822 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5823
5824 }
5825}
5826
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005827static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005828{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005829 u8 mode = 0;
5830
5831 if (cpu_has_secondary_exec_ctrls() &&
5832 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5833 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5834 mode |= MSR_BITMAP_MODE_X2APIC;
5835 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5836 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5837 }
5838
5839 if (is_long_mode(vcpu))
5840 mode |= MSR_BITMAP_MODE_LM;
5841
5842 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005843}
5844
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005845#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5846
5847static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5848 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005849{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005850 int msr;
5851
5852 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5853 unsigned word = msr / BITS_PER_LONG;
5854 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5855 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005856 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005857
5858 if (mode & MSR_BITMAP_MODE_X2APIC) {
5859 /*
5860 * TPR reads and writes can be virtualized even if virtual interrupt
5861 * delivery is not in use.
5862 */
5863 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5864 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5865 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5866 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5867 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5868 }
5869 }
5870}
5871
5872static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5873{
5874 struct vcpu_vmx *vmx = to_vmx(vcpu);
5875 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5876 u8 mode = vmx_msr_bitmap_mode(vcpu);
5877 u8 changed = mode ^ vmx->msr_bitmap_mode;
5878
5879 if (!changed)
5880 return;
5881
5882 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5883 !(mode & MSR_BITMAP_MODE_LM));
5884
5885 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5886 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5887
5888 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005889}
5890
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005891static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005892{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005893 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005894}
5895
David Matlackc9f04402017-08-01 14:00:40 -07005896static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5897{
5898 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5899 gfn_t gfn;
5900
5901 /*
5902 * Don't need to mark the APIC access page dirty; it is never
5903 * written to by the CPU during APIC virtualization.
5904 */
5905
5906 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5907 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5908 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5909 }
5910
5911 if (nested_cpu_has_posted_intr(vmcs12)) {
5912 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5913 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5914 }
5915}
5916
5917
David Hildenbrand6342c502017-01-25 11:58:58 +01005918static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005919{
5920 struct vcpu_vmx *vmx = to_vmx(vcpu);
5921 int max_irr;
5922 void *vapic_page;
5923 u16 status;
5924
David Matlackc9f04402017-08-01 14:00:40 -07005925 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5926 return;
Wincy Van705699a2015-02-03 23:58:17 +08005927
David Matlackc9f04402017-08-01 14:00:40 -07005928 vmx->nested.pi_pending = false;
5929 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5930 return;
Wincy Van705699a2015-02-03 23:58:17 +08005931
David Matlackc9f04402017-08-01 14:00:40 -07005932 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5933 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005934 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005935 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5936 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005937 kunmap(vmx->nested.virtual_apic_page);
5938
5939 status = vmcs_read16(GUEST_INTR_STATUS);
5940 if ((u8)max_irr > ((u8)status & 0xff)) {
5941 status &= ~0xff;
5942 status |= (u8)max_irr;
5943 vmcs_write16(GUEST_INTR_STATUS, status);
5944 }
5945 }
David Matlackc9f04402017-08-01 14:00:40 -07005946
5947 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005948}
5949
Wincy Van06a55242017-04-28 13:13:59 +08005950static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5951 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005952{
5953#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005954 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5955
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005956 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005957 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005958 * The vector of interrupt to be delivered to vcpu had
5959 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005960 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005961 * Following cases will be reached in this block, and
5962 * we always send a notification event in all cases as
5963 * explained below.
5964 *
5965 * Case 1: vcpu keeps in non-root mode. Sending a
5966 * notification event posts the interrupt to vcpu.
5967 *
5968 * Case 2: vcpu exits to root mode and is still
5969 * runnable. PIR will be synced to vIRR before the
5970 * next vcpu entry. Sending a notification event in
5971 * this case has no effect, as vcpu is not in root
5972 * mode.
5973 *
5974 * Case 3: vcpu exits to root mode and is blocked.
5975 * vcpu_block() has already synced PIR to vIRR and
5976 * never blocks vcpu if vIRR is not cleared. Therefore,
5977 * a blocked vcpu here does not wait for any requested
5978 * interrupts in PIR, and sending a notification event
5979 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005980 */
Feng Wu28b835d2015-09-18 22:29:54 +08005981
Wincy Van06a55242017-04-28 13:13:59 +08005982 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005983 return true;
5984 }
5985#endif
5986 return false;
5987}
5988
Wincy Van705699a2015-02-03 23:58:17 +08005989static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5990 int vector)
5991{
5992 struct vcpu_vmx *vmx = to_vmx(vcpu);
5993
5994 if (is_guest_mode(vcpu) &&
5995 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005996 /*
5997 * If a posted intr is not recognized by hardware,
5998 * we will accomplish it in the next vmentry.
5999 */
6000 vmx->nested.pi_pending = true;
6001 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006002 /* the PIR and ON have been set by L1. */
6003 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6004 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006005 return 0;
6006 }
6007 return -1;
6008}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006009/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006010 * Send interrupt to vcpu via posted interrupt way.
6011 * 1. If target vcpu is running(non-root mode), send posted interrupt
6012 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6013 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6014 * interrupt from PIR in next vmentry.
6015 */
6016static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6017{
6018 struct vcpu_vmx *vmx = to_vmx(vcpu);
6019 int r;
6020
Wincy Van705699a2015-02-03 23:58:17 +08006021 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6022 if (!r)
6023 return;
6024
Yang Zhanga20ed542013-04-11 19:25:15 +08006025 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6026 return;
6027
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006028 /* If a previous notification has sent the IPI, nothing to do. */
6029 if (pi_test_and_set_on(&vmx->pi_desc))
6030 return;
6031
Wincy Van06a55242017-04-28 13:13:59 +08006032 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006033 kvm_vcpu_kick(vcpu);
6034}
6035
Avi Kivity6aa8b732006-12-10 02:21:36 -08006036/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006037 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6038 * will not change in the lifetime of the guest.
6039 * Note that host-state that does change is set elsewhere. E.g., host-state
6040 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6041 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006042static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006043{
6044 u32 low32, high32;
6045 unsigned long tmpl;
6046 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006047 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006048
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006049 cr0 = read_cr0();
6050 WARN_ON(cr0 & X86_CR0_TS);
6051 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006052
6053 /*
6054 * Save the most likely value for this task's CR3 in the VMCS.
6055 * We can't use __get_current_cr3_fast() because we're not atomic.
6056 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006057 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006058 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02006059 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006060
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006061 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006062 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006063 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02006064 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006065
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006066 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006067#ifdef CONFIG_X86_64
6068 /*
6069 * Load null selectors, so we can avoid reloading them in
6070 * __vmx_load_host_state(), in case userspace uses the null selectors
6071 * too (the expected case).
6072 */
6073 vmcs_write16(HOST_DS_SELECTOR, 0);
6074 vmcs_write16(HOST_ES_SELECTOR, 0);
6075#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006076 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6077 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006078#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006079 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6080 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6081
Juergen Gross87930012017-09-04 12:25:27 +02006082 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006083 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006084 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006085
Avi Kivity83287ea422012-09-16 15:10:57 +03006086 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006087
6088 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6089 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6090 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6091 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6092
6093 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6094 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6095 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6096 }
6097}
6098
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006099static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6100{
6101 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6102 if (enable_ept)
6103 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006104 if (is_guest_mode(&vmx->vcpu))
6105 vmx->vcpu.arch.cr4_guest_owned_bits &=
6106 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006107 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6108}
6109
Yang Zhang01e439b2013-04-11 19:25:12 +08006110static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6111{
6112 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6113
Andrey Smetanind62caab2015-11-10 15:36:33 +03006114 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006115 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006116
6117 if (!enable_vnmi)
6118 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6119
Yunhong Jiang64672c92016-06-13 14:19:59 -07006120 /* Enable the preemption timer dynamically */
6121 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006122 return pin_based_exec_ctrl;
6123}
6124
Andrey Smetanind62caab2015-11-10 15:36:33 +03006125static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6126{
6127 struct vcpu_vmx *vmx = to_vmx(vcpu);
6128
6129 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006130 if (cpu_has_secondary_exec_ctrls()) {
6131 if (kvm_vcpu_apicv_active(vcpu))
6132 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6133 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6134 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6135 else
6136 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6137 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6138 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6139 }
6140
6141 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006142 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006143}
6144
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006145static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6146{
6147 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006148
6149 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6150 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6151
Paolo Bonzini35754c92015-07-29 12:05:37 +02006152 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006153 exec_control &= ~CPU_BASED_TPR_SHADOW;
6154#ifdef CONFIG_X86_64
6155 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6156 CPU_BASED_CR8_LOAD_EXITING;
6157#endif
6158 }
6159 if (!enable_ept)
6160 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6161 CPU_BASED_CR3_LOAD_EXITING |
6162 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006163 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6164 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6165 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006166 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6167 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006168 return exec_control;
6169}
6170
Jim Mattson45ec3682017-08-23 16:32:04 -07006171static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006172{
Jim Mattson45ec3682017-08-23 16:32:04 -07006173 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006174 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006175}
6176
Jim Mattson75f4fc82017-08-23 16:32:03 -07006177static bool vmx_rdseed_supported(void)
6178{
6179 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006180 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006181}
6182
Paolo Bonzini80154d72017-08-24 13:55:35 +02006183static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006184{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006185 struct kvm_vcpu *vcpu = &vmx->vcpu;
6186
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006187 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006188
Paolo Bonzini80154d72017-08-24 13:55:35 +02006189 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006190 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6191 if (vmx->vpid == 0)
6192 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6193 if (!enable_ept) {
6194 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6195 enable_unrestricted_guest = 0;
6196 }
6197 if (!enable_unrestricted_guest)
6198 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006199 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006200 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006201 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006202 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6203 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006204 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006205
6206 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6207 * in vmx_set_cr4. */
6208 exec_control &= ~SECONDARY_EXEC_DESC;
6209
Abel Gordonabc4fc52013-04-18 14:35:25 +03006210 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6211 (handle_vmptrld).
6212 We can NOT enable shadow_vmcs here because we don't have yet
6213 a current VMCS12
6214 */
6215 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006216
6217 if (!enable_pml)
6218 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006219
Paolo Bonzini3db13482017-08-24 14:48:03 +02006220 if (vmx_xsaves_supported()) {
6221 /* Exposing XSAVES only when XSAVE is exposed */
6222 bool xsaves_enabled =
6223 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6224 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6225
6226 if (!xsaves_enabled)
6227 exec_control &= ~SECONDARY_EXEC_XSAVES;
6228
6229 if (nested) {
6230 if (xsaves_enabled)
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 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006234 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006235 ~SECONDARY_EXEC_XSAVES;
6236 }
6237 }
6238
Paolo Bonzini80154d72017-08-24 13:55:35 +02006239 if (vmx_rdtscp_supported()) {
6240 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6241 if (!rdtscp_enabled)
6242 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6243
6244 if (nested) {
6245 if (rdtscp_enabled)
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 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006249 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006250 ~SECONDARY_EXEC_RDTSCP;
6251 }
6252 }
6253
6254 if (vmx_invpcid_supported()) {
6255 /* Exposing INVPCID only when PCID is exposed */
6256 bool invpcid_enabled =
6257 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6258 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6259
6260 if (!invpcid_enabled) {
6261 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6262 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6263 }
6264
6265 if (nested) {
6266 if (invpcid_enabled)
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 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006270 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006271 ~SECONDARY_EXEC_ENABLE_INVPCID;
6272 }
6273 }
6274
Jim Mattson45ec3682017-08-23 16:32:04 -07006275 if (vmx_rdrand_supported()) {
6276 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6277 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006278 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006279
6280 if (nested) {
6281 if (rdrand_enabled)
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 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006285 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006286 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006287 }
6288 }
6289
Jim Mattson75f4fc82017-08-23 16:32:03 -07006290 if (vmx_rdseed_supported()) {
6291 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6292 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006293 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006294
6295 if (nested) {
6296 if (rdseed_enabled)
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 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006300 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006301 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006302 }
6303 }
6304
Paolo Bonzini80154d72017-08-24 13:55:35 +02006305 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006306}
6307
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006308static void ept_set_mmio_spte_mask(void)
6309{
6310 /*
6311 * EPT Misconfigurations can be generated if the value of bits 2:0
6312 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006313 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006314 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6315 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006316}
6317
Wanpeng Lif53cd632014-12-02 19:14:58 +08006318#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006319/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006320 * Sets up the vmcs for emulated real mode.
6321 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006322static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006323{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006324#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006325 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006326#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006327 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006328
Abel Gordon4607c2d2013-04-18 14:35:55 +03006329 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006330 /*
6331 * At vCPU creation, "VMWRITE to any supported field
6332 * in the VMCS" is supported, so use the more
6333 * permissive vmx_vmread_bitmap to specify both read
6334 * and write permissions for the shadow VMCS.
6335 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006336 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006337 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006338 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006339 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006340 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006341
Avi Kivity6aa8b732006-12-10 02:21:36 -08006342 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6343
Avi Kivity6aa8b732006-12-10 02:21:36 -08006344 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006345 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006346 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006347
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006348 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006349
Dan Williamsdfa169b2016-06-02 11:17:24 -07006350 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006351 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006352 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006353 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006354 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006355
Andrey Smetanind62caab2015-11-10 15:36:33 +03006356 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006357 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6358 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6359 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6360 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6361
6362 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006363
Li RongQing0bcf2612015-12-03 13:29:34 +08006364 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006365 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006366 }
6367
Wanpeng Lib31c1142018-03-12 04:53:04 -07006368 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006369 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006370 vmx->ple_window = ple_window;
6371 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006372 }
6373
Xiao Guangrongc3707952011-07-12 03:28:04 +08006374 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6375 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006376 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6377
Avi Kivity9581d442010-10-19 16:46:55 +02006378 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6379 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006380 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006381#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006382 rdmsrl(MSR_FS_BASE, a);
6383 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6384 rdmsrl(MSR_GS_BASE, a);
6385 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6386#else
6387 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6388 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6389#endif
6390
Bandan Das2a499e42017-08-03 15:54:41 -04006391 if (cpu_has_vmx_vmfunc())
6392 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6393
Eddie Dong2cc51562007-05-21 07:28:09 +03006394 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6395 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006396 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006397 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006398 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006399
Radim Krčmář74545702015-04-27 15:11:25 +02006400 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6401 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006402
Paolo Bonzini03916db2014-07-24 14:21:57 +02006403 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006404 u32 index = vmx_msr_index[i];
6405 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006406 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006407
6408 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6409 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006410 if (wrmsr_safe(index, data_low, data_high) < 0)
6411 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006412 vmx->guest_msrs[j].index = i;
6413 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006414 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006415 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006416 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006417
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006418 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6419 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006420
6421 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006422
6423 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006424 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006425
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006426 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6427 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6428
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006429 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006430
Wanpeng Lif53cd632014-12-02 19:14:58 +08006431 if (vmx_xsaves_supported())
6432 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6433
Peter Feiner4e595162016-07-07 14:49:58 -07006434 if (enable_pml) {
6435 ASSERT(vmx->pml_pg);
6436 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6437 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6438 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006439}
6440
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006441static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006442{
6443 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006444 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006445 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006446
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006447 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006448 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006449
Wanpeng Li518e7b92018-02-28 14:03:31 +08006450 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006451 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006452 kvm_set_cr8(vcpu, 0);
6453
6454 if (!init_event) {
6455 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6456 MSR_IA32_APICBASE_ENABLE;
6457 if (kvm_vcpu_is_reset_bsp(vcpu))
6458 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6459 apic_base_msr.host_initiated = true;
6460 kvm_set_apic_base(vcpu, &apic_base_msr);
6461 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006462
Avi Kivity2fb92db2011-04-27 19:42:18 +03006463 vmx_segment_cache_clear(vmx);
6464
Avi Kivity5706be02008-08-20 15:07:31 +03006465 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006466 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006467 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006468
6469 seg_setup(VCPU_SREG_DS);
6470 seg_setup(VCPU_SREG_ES);
6471 seg_setup(VCPU_SREG_FS);
6472 seg_setup(VCPU_SREG_GS);
6473 seg_setup(VCPU_SREG_SS);
6474
6475 vmcs_write16(GUEST_TR_SELECTOR, 0);
6476 vmcs_writel(GUEST_TR_BASE, 0);
6477 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6478 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6479
6480 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6481 vmcs_writel(GUEST_LDTR_BASE, 0);
6482 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6483 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6484
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006485 if (!init_event) {
6486 vmcs_write32(GUEST_SYSENTER_CS, 0);
6487 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6488 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6489 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6490 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006491
Wanpeng Lic37c2872017-11-20 14:52:21 -08006492 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006493 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006494
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006495 vmcs_writel(GUEST_GDTR_BASE, 0);
6496 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6497
6498 vmcs_writel(GUEST_IDTR_BASE, 0);
6499 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6500
Anthony Liguori443381a2010-12-06 10:53:38 -06006501 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006502 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006503 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006504 if (kvm_mpx_supported())
6505 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006506
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006507 setup_msrs(vmx);
6508
Avi Kivity6aa8b732006-12-10 02:21:36 -08006509 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6510
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006511 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006512 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006513 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006514 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006515 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006516 vmcs_write32(TPR_THRESHOLD, 0);
6517 }
6518
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006519 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006520
Sheng Yang2384d2b2008-01-17 15:14:33 +08006521 if (vmx->vpid != 0)
6522 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6523
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006524 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006525 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006526 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006527 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006528 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006529
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006530 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006531
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006532 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006533 if (init_event)
6534 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006535}
6536
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006537/*
6538 * In nested virtualization, check if L1 asked to exit on external interrupts.
6539 * For most existing hypervisors, this will always return true.
6540 */
6541static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6542{
6543 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6544 PIN_BASED_EXT_INTR_MASK;
6545}
6546
Bandan Das77b0f5d2014-04-19 18:17:45 -04006547/*
6548 * In nested virtualization, check if L1 has set
6549 * VM_EXIT_ACK_INTR_ON_EXIT
6550 */
6551static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6552{
6553 return get_vmcs12(vcpu)->vm_exit_controls &
6554 VM_EXIT_ACK_INTR_ON_EXIT;
6555}
6556
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006557static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6558{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006559 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006560}
6561
Jan Kiszkac9a79532014-03-07 20:03:15 +01006562static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006563{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006564 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6565 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006566}
6567
Jan Kiszkac9a79532014-03-07 20:03:15 +01006568static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006569{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006570 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006571 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006572 enable_irq_window(vcpu);
6573 return;
6574 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006575
Paolo Bonzini47c01522016-12-19 11:44:07 +01006576 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6577 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006578}
6579
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006580static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006581{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006582 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006583 uint32_t intr;
6584 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006585
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006586 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006587
Avi Kivityfa89a812008-09-01 15:57:51 +03006588 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006589 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006590 int inc_eip = 0;
6591 if (vcpu->arch.interrupt.soft)
6592 inc_eip = vcpu->arch.event_exit_inst_len;
6593 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006594 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006595 return;
6596 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006597 intr = irq | INTR_INFO_VALID_MASK;
6598 if (vcpu->arch.interrupt.soft) {
6599 intr |= INTR_TYPE_SOFT_INTR;
6600 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6601 vmx->vcpu.arch.event_exit_inst_len);
6602 } else
6603 intr |= INTR_TYPE_EXT_INTR;
6604 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006605
6606 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006607}
6608
Sheng Yangf08864b2008-05-15 18:23:25 +08006609static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6610{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006611 struct vcpu_vmx *vmx = to_vmx(vcpu);
6612
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006613 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006614 /*
6615 * Tracking the NMI-blocked state in software is built upon
6616 * finding the next open IRQ window. This, in turn, depends on
6617 * well-behaving guests: They have to keep IRQs disabled at
6618 * least as long as the NMI handler runs. Otherwise we may
6619 * cause NMI nesting, maybe breaking the guest. But as this is
6620 * highly unlikely, we can live with the residual risk.
6621 */
6622 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6623 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6624 }
6625
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006626 ++vcpu->stat.nmi_injections;
6627 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006628
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006629 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006630 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006631 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006632 return;
6633 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006634
Sheng Yangf08864b2008-05-15 18:23:25 +08006635 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6636 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006637
6638 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006639}
6640
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006641static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6642{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006643 struct vcpu_vmx *vmx = to_vmx(vcpu);
6644 bool masked;
6645
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006646 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006647 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006648 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006649 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006650 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6651 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6652 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006653}
6654
6655static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6656{
6657 struct vcpu_vmx *vmx = to_vmx(vcpu);
6658
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006659 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006660 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6661 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6662 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6663 }
6664 } else {
6665 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6666 if (masked)
6667 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6668 GUEST_INTR_STATE_NMI);
6669 else
6670 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6671 GUEST_INTR_STATE_NMI);
6672 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006673}
6674
Jan Kiszka2505dc92013-04-14 12:12:47 +02006675static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6676{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006677 if (to_vmx(vcpu)->nested.nested_run_pending)
6678 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006679
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006680 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006681 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6682 return 0;
6683
Jan Kiszka2505dc92013-04-14 12:12:47 +02006684 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6685 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6686 | GUEST_INTR_STATE_NMI));
6687}
6688
Gleb Natapov78646122009-03-23 12:12:11 +02006689static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6690{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006691 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6692 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006693 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6694 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006695}
6696
Izik Eiduscbc94022007-10-25 00:29:55 +02006697static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6698{
6699 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006700
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006701 if (enable_unrestricted_guest)
6702 return 0;
6703
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006704 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6705 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006706 if (ret)
6707 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006708 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006709 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006710}
6711
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006712static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6713{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006714 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006715 return 0;
6716}
6717
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006718static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006719{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006720 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006721 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006722 /*
6723 * Update instruction length as we may reinject the exception
6724 * from user space while in guest debugging mode.
6725 */
6726 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6727 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006728 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006729 return false;
6730 /* fall through */
6731 case DB_VECTOR:
6732 if (vcpu->guest_debug &
6733 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6734 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006735 /* fall through */
6736 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006737 case OF_VECTOR:
6738 case BR_VECTOR:
6739 case UD_VECTOR:
6740 case DF_VECTOR:
6741 case SS_VECTOR:
6742 case GP_VECTOR:
6743 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006744 return true;
6745 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006746 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006747 return false;
6748}
6749
6750static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6751 int vec, u32 err_code)
6752{
6753 /*
6754 * Instruction with address size override prefix opcode 0x67
6755 * Cause the #SS fault with 0 error code in VM86 mode.
6756 */
6757 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6758 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6759 if (vcpu->arch.halt_request) {
6760 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006761 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006762 }
6763 return 1;
6764 }
6765 return 0;
6766 }
6767
6768 /*
6769 * Forward all other exceptions that are valid in real mode.
6770 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6771 * the required debugging infrastructure rework.
6772 */
6773 kvm_queue_exception(vcpu, vec);
6774 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006775}
6776
Andi Kleena0861c02009-06-08 17:37:09 +08006777/*
6778 * Trigger machine check on the host. We assume all the MSRs are already set up
6779 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6780 * We pass a fake environment to the machine check handler because we want
6781 * the guest to be always treated like user space, no matter what context
6782 * it used internally.
6783 */
6784static void kvm_machine_check(void)
6785{
6786#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6787 struct pt_regs regs = {
6788 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6789 .flags = X86_EFLAGS_IF,
6790 };
6791
6792 do_machine_check(&regs, 0);
6793#endif
6794}
6795
Avi Kivity851ba692009-08-24 11:10:17 +03006796static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006797{
6798 /* already handled by vcpu_run */
6799 return 1;
6800}
6801
Avi Kivity851ba692009-08-24 11:10:17 +03006802static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006803{
Avi Kivity1155f762007-11-22 11:30:47 +02006804 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006805 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006806 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006807 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006808 u32 vect_info;
6809 enum emulation_result er;
6810
Avi Kivity1155f762007-11-22 11:30:47 +02006811 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006812 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006813
Andi Kleena0861c02009-06-08 17:37:09 +08006814 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006815 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006816
Jim Mattsonef85b672016-12-12 11:01:37 -08006817 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006818 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006819
Wanpeng Li082d06e2018-04-03 16:28:48 -07006820 if (is_invalid_opcode(intr_info))
6821 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006822
Avi Kivity6aa8b732006-12-10 02:21:36 -08006823 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006824 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006825 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006826
Liran Alon9e869482018-03-12 13:12:51 +02006827 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6828 WARN_ON_ONCE(!enable_vmware_backdoor);
6829 er = emulate_instruction(vcpu,
6830 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6831 if (er == EMULATE_USER_EXIT)
6832 return 0;
6833 else if (er != EMULATE_DONE)
6834 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6835 return 1;
6836 }
6837
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006838 /*
6839 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6840 * MMIO, it is better to report an internal error.
6841 * See the comments in vmx_handle_exit.
6842 */
6843 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6844 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6845 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6846 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006847 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006848 vcpu->run->internal.data[0] = vect_info;
6849 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006850 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006851 return 0;
6852 }
6853
Avi Kivity6aa8b732006-12-10 02:21:36 -08006854 if (is_page_fault(intr_info)) {
6855 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006856 /* EPT won't cause page fault directly */
6857 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006858 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006859 }
6860
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006861 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006862
6863 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6864 return handle_rmode_exception(vcpu, ex_no, error_code);
6865
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006866 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006867 case AC_VECTOR:
6868 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6869 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006870 case DB_VECTOR:
6871 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6872 if (!(vcpu->guest_debug &
6873 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006874 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006875 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006876 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006877 skip_emulated_instruction(vcpu);
6878
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006879 kvm_queue_exception(vcpu, DB_VECTOR);
6880 return 1;
6881 }
6882 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6883 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6884 /* fall through */
6885 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006886 /*
6887 * Update instruction length as we may reinject #BP from
6888 * user space while in guest debugging mode. Reading it for
6889 * #DB as well causes no harm, it is not used in that case.
6890 */
6891 vmx->vcpu.arch.event_exit_inst_len =
6892 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006893 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006894 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006895 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6896 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006897 break;
6898 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006899 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6900 kvm_run->ex.exception = ex_no;
6901 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006902 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006903 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006904 return 0;
6905}
6906
Avi Kivity851ba692009-08-24 11:10:17 +03006907static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006908{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006909 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006910 return 1;
6911}
6912
Avi Kivity851ba692009-08-24 11:10:17 +03006913static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006914{
Avi Kivity851ba692009-08-24 11:10:17 +03006915 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006916 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006917 return 0;
6918}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006919
Avi Kivity851ba692009-08-24 11:10:17 +03006920static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006921{
He, Qingbfdaab02007-09-12 14:18:28 +08006922 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006923 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006924 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006925
He, Qingbfdaab02007-09-12 14:18:28 +08006926 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006927 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006928
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006929 ++vcpu->stat.io_exits;
6930
Sean Christopherson432baf62018-03-08 08:57:26 -08006931 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006932 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006933
6934 port = exit_qualification >> 16;
6935 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006936 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006937
Sean Christophersondca7f122018-03-08 08:57:27 -08006938 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006939}
6940
Ingo Molnar102d8322007-02-19 14:37:47 +02006941static void
6942vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6943{
6944 /*
6945 * Patch in the VMCALL instruction:
6946 */
6947 hypercall[0] = 0x0f;
6948 hypercall[1] = 0x01;
6949 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006950}
6951
Guo Chao0fa06072012-06-28 15:16:19 +08006952/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006953static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6954{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006955 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006956 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6957 unsigned long orig_val = val;
6958
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006959 /*
6960 * We get here when L2 changed cr0 in a way that did not change
6961 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006962 * but did change L0 shadowed bits. So we first calculate the
6963 * effective cr0 value that L1 would like to write into the
6964 * hardware. It consists of the L2-owned bits from the new
6965 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006966 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006967 val = (val & ~vmcs12->cr0_guest_host_mask) |
6968 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6969
David Matlack38991522016-11-29 18:14:08 -08006970 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006971 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006972
6973 if (kvm_set_cr0(vcpu, val))
6974 return 1;
6975 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006976 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006977 } else {
6978 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006979 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006980 return 1;
David Matlack38991522016-11-29 18:14:08 -08006981
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006982 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006983 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006984}
6985
6986static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6987{
6988 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006989 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6990 unsigned long orig_val = val;
6991
6992 /* analogously to handle_set_cr0 */
6993 val = (val & ~vmcs12->cr4_guest_host_mask) |
6994 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6995 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006996 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006997 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006998 return 0;
6999 } else
7000 return kvm_set_cr4(vcpu, val);
7001}
7002
Paolo Bonzini0367f202016-07-12 10:44:55 +02007003static int handle_desc(struct kvm_vcpu *vcpu)
7004{
7005 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7006 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7007}
7008
Avi Kivity851ba692009-08-24 11:10:17 +03007009static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007010{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007011 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007012 int cr;
7013 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007014 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007015 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007016
He, Qingbfdaab02007-09-12 14:18:28 +08007017 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007018 cr = exit_qualification & 15;
7019 reg = (exit_qualification >> 8) & 15;
7020 switch ((exit_qualification >> 4) & 3) {
7021 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007022 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007023 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007024 switch (cr) {
7025 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007026 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007027 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007028 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007029 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007030 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007031 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007032 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007033 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007034 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007035 case 8: {
7036 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007037 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007038 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007039 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007040 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007041 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007042 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007043 return ret;
7044 /*
7045 * TODO: we might be squashing a
7046 * KVM_GUESTDBG_SINGLESTEP-triggered
7047 * KVM_EXIT_DEBUG here.
7048 */
Avi Kivity851ba692009-08-24 11:10:17 +03007049 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007050 return 0;
7051 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007052 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007053 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007054 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007055 WARN_ONCE(1, "Guest should always own CR0.TS");
7056 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007057 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007058 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007059 case 1: /*mov from cr*/
7060 switch (cr) {
7061 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007062 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007063 val = kvm_read_cr3(vcpu);
7064 kvm_register_write(vcpu, reg, val);
7065 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007066 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007067 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007068 val = kvm_get_cr8(vcpu);
7069 kvm_register_write(vcpu, reg, val);
7070 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007071 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007072 }
7073 break;
7074 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007075 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007076 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007077 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007078
Kyle Huey6affcbe2016-11-29 12:40:40 -08007079 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007080 default:
7081 break;
7082 }
Avi Kivity851ba692009-08-24 11:10:17 +03007083 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007084 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007085 (int)(exit_qualification >> 4) & 3, cr);
7086 return 0;
7087}
7088
Avi Kivity851ba692009-08-24 11:10:17 +03007089static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007090{
He, Qingbfdaab02007-09-12 14:18:28 +08007091 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007092 int dr, dr7, reg;
7093
7094 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7095 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7096
7097 /* First, if DR does not exist, trigger UD */
7098 if (!kvm_require_dr(vcpu, dr))
7099 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007100
Jan Kiszkaf2483412010-01-20 18:20:20 +01007101 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007102 if (!kvm_require_cpl(vcpu, 0))
7103 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007104 dr7 = vmcs_readl(GUEST_DR7);
7105 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007106 /*
7107 * As the vm-exit takes precedence over the debug trap, we
7108 * need to emulate the latter, either for the host or the
7109 * guest debugging itself.
7110 */
7111 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007112 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007113 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007114 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007115 vcpu->run->debug.arch.exception = DB_VECTOR;
7116 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007117 return 0;
7118 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007119 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007120 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007121 kvm_queue_exception(vcpu, DB_VECTOR);
7122 return 1;
7123 }
7124 }
7125
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007126 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007127 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7128 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007129
7130 /*
7131 * No more DR vmexits; force a reload of the debug registers
7132 * and reenter on this instruction. The next vmexit will
7133 * retrieve the full state of the debug registers.
7134 */
7135 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7136 return 1;
7137 }
7138
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007139 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7140 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007141 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007142
7143 if (kvm_get_dr(vcpu, dr, &val))
7144 return 1;
7145 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007146 } else
Nadav Amit57773922014-06-18 17:19:23 +03007147 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007148 return 1;
7149
Kyle Huey6affcbe2016-11-29 12:40:40 -08007150 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007151}
7152
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007153static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7154{
7155 return vcpu->arch.dr6;
7156}
7157
7158static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7159{
7160}
7161
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007162static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7163{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007164 get_debugreg(vcpu->arch.db[0], 0);
7165 get_debugreg(vcpu->arch.db[1], 1);
7166 get_debugreg(vcpu->arch.db[2], 2);
7167 get_debugreg(vcpu->arch.db[3], 3);
7168 get_debugreg(vcpu->arch.dr6, 6);
7169 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7170
7171 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007172 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007173}
7174
Gleb Natapov020df072010-04-13 10:05:23 +03007175static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7176{
7177 vmcs_writel(GUEST_DR7, val);
7178}
7179
Avi Kivity851ba692009-08-24 11:10:17 +03007180static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007181{
Kyle Huey6a908b62016-11-29 12:40:37 -08007182 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007183}
7184
Avi Kivity851ba692009-08-24 11:10:17 +03007185static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007186{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007187 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007188 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007189
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007190 msr_info.index = ecx;
7191 msr_info.host_initiated = false;
7192 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007193 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007194 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007195 return 1;
7196 }
7197
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007198 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007199
Avi Kivity6aa8b732006-12-10 02:21:36 -08007200 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007201 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7202 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007203 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007204}
7205
Avi Kivity851ba692009-08-24 11:10:17 +03007206static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007207{
Will Auld8fe8ab42012-11-29 12:42:12 -08007208 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007209 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7210 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7211 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007212
Will Auld8fe8ab42012-11-29 12:42:12 -08007213 msr.data = data;
7214 msr.index = ecx;
7215 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007216 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007217 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007218 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007219 return 1;
7220 }
7221
Avi Kivity59200272010-01-25 19:47:02 +02007222 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007223 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007224}
7225
Avi Kivity851ba692009-08-24 11:10:17 +03007226static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007227{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007228 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007229 return 1;
7230}
7231
Avi Kivity851ba692009-08-24 11:10:17 +03007232static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007233{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007234 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7235 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007236
Avi Kivity3842d132010-07-27 12:30:24 +03007237 kvm_make_request(KVM_REQ_EVENT, vcpu);
7238
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007239 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007240 return 1;
7241}
7242
Avi Kivity851ba692009-08-24 11:10:17 +03007243static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007244{
Avi Kivityd3bef152007-06-05 15:53:05 +03007245 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007246}
7247
Avi Kivity851ba692009-08-24 11:10:17 +03007248static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007249{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007250 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007251}
7252
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007253static int handle_invd(struct kvm_vcpu *vcpu)
7254{
Andre Przywara51d8b662010-12-21 11:12:02 +01007255 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007256}
7257
Avi Kivity851ba692009-08-24 11:10:17 +03007258static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007259{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007260 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007261
7262 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007263 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007264}
7265
Avi Kivityfee84b02011-11-10 14:57:25 +02007266static int handle_rdpmc(struct kvm_vcpu *vcpu)
7267{
7268 int err;
7269
7270 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007271 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007272}
7273
Avi Kivity851ba692009-08-24 11:10:17 +03007274static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007275{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007276 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007277}
7278
Dexuan Cui2acf9232010-06-10 11:27:12 +08007279static int handle_xsetbv(struct kvm_vcpu *vcpu)
7280{
7281 u64 new_bv = kvm_read_edx_eax(vcpu);
7282 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7283
7284 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007285 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007286 return 1;
7287}
7288
Wanpeng Lif53cd632014-12-02 19:14:58 +08007289static int handle_xsaves(struct kvm_vcpu *vcpu)
7290{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007291 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007292 WARN(1, "this should never happen\n");
7293 return 1;
7294}
7295
7296static int handle_xrstors(struct kvm_vcpu *vcpu)
7297{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007298 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007299 WARN(1, "this should never happen\n");
7300 return 1;
7301}
7302
Avi Kivity851ba692009-08-24 11:10:17 +03007303static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007304{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007305 if (likely(fasteoi)) {
7306 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7307 int access_type, offset;
7308
7309 access_type = exit_qualification & APIC_ACCESS_TYPE;
7310 offset = exit_qualification & APIC_ACCESS_OFFSET;
7311 /*
7312 * Sane guest uses MOV to write EOI, with written value
7313 * not cared. So make a short-circuit here by avoiding
7314 * heavy instruction emulation.
7315 */
7316 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7317 (offset == APIC_EOI)) {
7318 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007319 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007320 }
7321 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007322 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007323}
7324
Yang Zhangc7c9c562013-01-25 10:18:51 +08007325static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7326{
7327 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7328 int vector = exit_qualification & 0xff;
7329
7330 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7331 kvm_apic_set_eoi_accelerated(vcpu, vector);
7332 return 1;
7333}
7334
Yang Zhang83d4c282013-01-25 10:18:49 +08007335static int handle_apic_write(struct kvm_vcpu *vcpu)
7336{
7337 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7338 u32 offset = exit_qualification & 0xfff;
7339
7340 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7341 kvm_apic_write_nodecode(vcpu, offset);
7342 return 1;
7343}
7344
Avi Kivity851ba692009-08-24 11:10:17 +03007345static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007346{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007347 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007348 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007349 bool has_error_code = false;
7350 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007351 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007352 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007353
7354 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007355 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007356 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007357
7358 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7359
7360 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007361 if (reason == TASK_SWITCH_GATE && idt_v) {
7362 switch (type) {
7363 case INTR_TYPE_NMI_INTR:
7364 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007365 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007366 break;
7367 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007368 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007369 kvm_clear_interrupt_queue(vcpu);
7370 break;
7371 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007372 if (vmx->idt_vectoring_info &
7373 VECTORING_INFO_DELIVER_CODE_MASK) {
7374 has_error_code = true;
7375 error_code =
7376 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7377 }
7378 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007379 case INTR_TYPE_SOFT_EXCEPTION:
7380 kvm_clear_exception_queue(vcpu);
7381 break;
7382 default:
7383 break;
7384 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007385 }
Izik Eidus37817f22008-03-24 23:14:53 +02007386 tss_selector = exit_qualification;
7387
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007388 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7389 type != INTR_TYPE_EXT_INTR &&
7390 type != INTR_TYPE_NMI_INTR))
7391 skip_emulated_instruction(vcpu);
7392
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007393 if (kvm_task_switch(vcpu, tss_selector,
7394 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7395 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007396 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7397 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7398 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007399 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007400 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007401
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007402 /*
7403 * TODO: What about debug traps on tss switch?
7404 * Are we supposed to inject them and update dr6?
7405 */
7406
7407 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007408}
7409
Avi Kivity851ba692009-08-24 11:10:17 +03007410static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007411{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007412 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007413 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007414 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007415
Sheng Yangf9c617f2009-03-25 10:08:52 +08007416 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007417
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007418 /*
7419 * EPT violation happened while executing iret from NMI,
7420 * "blocked by NMI" bit has to be set before next VM entry.
7421 * There are errata that may cause this bit to not be set:
7422 * AAK134, BY25.
7423 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007424 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007425 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007426 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007427 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7428
Sheng Yang14394422008-04-28 12:24:45 +08007429 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007430 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007431
Junaid Shahid27959a42016-12-06 16:46:10 -08007432 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007433 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007434 ? PFERR_USER_MASK : 0;
7435 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007436 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007437 ? PFERR_WRITE_MASK : 0;
7438 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007439 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007440 ? PFERR_FETCH_MASK : 0;
7441 /* ept page table entry is present? */
7442 error_code |= (exit_qualification &
7443 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7444 EPT_VIOLATION_EXECUTABLE))
7445 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007446
Paolo Bonzinieebed242016-11-28 14:39:58 +01007447 error_code |= (exit_qualification & 0x100) != 0 ?
7448 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007449
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007450 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007451 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007452}
7453
Avi Kivity851ba692009-08-24 11:10:17 +03007454static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007455{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007456 gpa_t gpa;
7457
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007458 /*
7459 * A nested guest cannot optimize MMIO vmexits, because we have an
7460 * nGPA here instead of the required GPA.
7461 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007462 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007463 if (!is_guest_mode(vcpu) &&
7464 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007465 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007466 /*
7467 * Doing kvm_skip_emulated_instruction() depends on undefined
7468 * behavior: Intel's manual doesn't mandate
7469 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7470 * occurs and while on real hardware it was observed to be set,
7471 * other hypervisors (namely Hyper-V) don't set it, we end up
7472 * advancing IP with some random value. Disable fast mmio when
7473 * running nested and keep it for real hardware in hope that
7474 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7475 */
7476 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7477 return kvm_skip_emulated_instruction(vcpu);
7478 else
7479 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7480 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007481 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007482
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007483 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007484}
7485
Avi Kivity851ba692009-08-24 11:10:17 +03007486static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007487{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007488 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007489 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7490 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007491 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007492 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007493
7494 return 1;
7495}
7496
Mohammed Gamal80ced182009-09-01 12:48:18 +02007497static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007498{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007499 struct vcpu_vmx *vmx = to_vmx(vcpu);
7500 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007501 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007502 u32 cpu_exec_ctrl;
7503 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007504 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007505
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007506 /*
7507 * We should never reach the point where we are emulating L2
7508 * due to invalid guest state as that means we incorrectly
7509 * allowed a nested VMEntry with an invalid vmcs12.
7510 */
7511 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7512
Avi Kivity49e9d552010-09-19 14:34:08 +02007513 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7514 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007515
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007516 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007517 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007518 return handle_interrupt_window(&vmx->vcpu);
7519
Radim Krčmář72875d82017-04-26 22:32:19 +02007520 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007521 return 1;
7522
Liran Alon9b8ae632017-11-05 16:56:34 +02007523 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007524
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007525 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007526 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007527 ret = 0;
7528 goto out;
7529 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007530
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007531 if (err != EMULATE_DONE)
7532 goto emulation_error;
7533
7534 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7535 vcpu->arch.exception.pending)
7536 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007537
Gleb Natapov8d76c492013-05-08 18:38:44 +03007538 if (vcpu->arch.halt_request) {
7539 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007540 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007541 goto out;
7542 }
7543
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007544 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007545 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007546 if (need_resched())
7547 schedule();
7548 }
7549
Mohammed Gamal80ced182009-09-01 12:48:18 +02007550out:
7551 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007552
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007553emulation_error:
7554 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7555 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7556 vcpu->run->internal.ndata = 0;
7557 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007558}
7559
7560static void grow_ple_window(struct kvm_vcpu *vcpu)
7561{
7562 struct vcpu_vmx *vmx = to_vmx(vcpu);
7563 int old = vmx->ple_window;
7564
Babu Mogerc8e88712018-03-16 16:37:24 -04007565 vmx->ple_window = __grow_ple_window(old, ple_window,
7566 ple_window_grow,
7567 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007568
7569 if (vmx->ple_window != old)
7570 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007571
7572 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007573}
7574
7575static void shrink_ple_window(struct kvm_vcpu *vcpu)
7576{
7577 struct vcpu_vmx *vmx = to_vmx(vcpu);
7578 int old = vmx->ple_window;
7579
Babu Mogerc8e88712018-03-16 16:37:24 -04007580 vmx->ple_window = __shrink_ple_window(old, ple_window,
7581 ple_window_shrink,
7582 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007583
7584 if (vmx->ple_window != old)
7585 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007586
7587 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007588}
7589
7590/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007591 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7592 */
7593static void wakeup_handler(void)
7594{
7595 struct kvm_vcpu *vcpu;
7596 int cpu = smp_processor_id();
7597
7598 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7599 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7600 blocked_vcpu_list) {
7601 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7602
7603 if (pi_test_on(pi_desc) == 1)
7604 kvm_vcpu_kick(vcpu);
7605 }
7606 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7607}
7608
Peng Haoe01bca22018-04-07 05:47:32 +08007609static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007610{
7611 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7612 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7613 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7614 0ull, VMX_EPT_EXECUTABLE_MASK,
7615 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007616 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007617
7618 ept_set_mmio_spte_mask();
7619 kvm_enable_tdp();
7620}
7621
Tiejun Chenf2c76482014-10-28 10:14:47 +08007622static __init int hardware_setup(void)
7623{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007624 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007625 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007626
7627 rdmsrl_safe(MSR_EFER, &host_efer);
7628
7629 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7630 kvm_define_shared_msr(i, vmx_msr_index[i]);
7631
Radim Krčmář23611332016-09-29 22:41:33 +02007632 for (i = 0; i < VMX_BITMAP_NR; i++) {
7633 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7634 if (!vmx_bitmap[i])
7635 goto out;
7636 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007637
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007638 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7639 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7640
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007641 if (setup_vmcs_config(&vmcs_config) < 0) {
7642 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007643 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007644 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007645
7646 if (boot_cpu_has(X86_FEATURE_NX))
7647 kvm_enable_efer_bits(EFER_NX);
7648
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007649 if (boot_cpu_has(X86_FEATURE_MPX)) {
7650 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7651 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7652 }
7653
Wanpeng Li08d839c2017-03-23 05:30:08 -07007654 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7655 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007656 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007657
Tiejun Chenf2c76482014-10-28 10:14:47 +08007658 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007659 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007660 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007661 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007662 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007663
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007664 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007665 enable_ept_ad_bits = 0;
7666
Wanpeng Li8ad81822017-10-09 15:51:53 -07007667 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007668 enable_unrestricted_guest = 0;
7669
Paolo Bonziniad15a292015-01-30 16:18:49 +01007670 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007671 flexpriority_enabled = 0;
7672
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007673 if (!cpu_has_virtual_nmis())
7674 enable_vnmi = 0;
7675
Paolo Bonziniad15a292015-01-30 16:18:49 +01007676 /*
7677 * set_apic_access_page_addr() is used to reload apic access
7678 * page upon invalidation. No need to do anything if not
7679 * using the APIC_ACCESS_ADDR VMCS field.
7680 */
7681 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007682 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007683
7684 if (!cpu_has_vmx_tpr_shadow())
7685 kvm_x86_ops->update_cr8_intercept = NULL;
7686
7687 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7688 kvm_disable_largepages();
7689
Tianyu Lan877ad952018-07-19 08:40:23 +00007690#if IS_ENABLED(CONFIG_HYPERV)
7691 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7692 && enable_ept)
7693 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7694#endif
7695
Wanpeng Li0f107682017-09-28 18:06:24 -07007696 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007697 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007698 ple_window = 0;
7699 ple_window_grow = 0;
7700 ple_window_max = 0;
7701 ple_window_shrink = 0;
7702 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007703
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007704 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007705 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007706 kvm_x86_ops->sync_pir_to_irr = NULL;
7707 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007708
Haozhong Zhang64903d62015-10-20 15:39:09 +08007709 if (cpu_has_vmx_tsc_scaling()) {
7710 kvm_has_tsc_control = true;
7711 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7712 kvm_tsc_scaling_ratio_frac_bits = 48;
7713 }
7714
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007715 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7716
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007717 if (enable_ept)
7718 vmx_enable_tdp();
7719 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007720 kvm_disable_tdp();
7721
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007722 if (!nested) {
7723 kvm_x86_ops->get_nested_state = NULL;
7724 kvm_x86_ops->set_nested_state = NULL;
7725 }
7726
Kai Huang843e4332015-01-28 10:54:28 +08007727 /*
7728 * Only enable PML when hardware supports PML feature, and both EPT
7729 * and EPT A/D bit features are enabled -- PML depends on them to work.
7730 */
7731 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7732 enable_pml = 0;
7733
7734 if (!enable_pml) {
7735 kvm_x86_ops->slot_enable_log_dirty = NULL;
7736 kvm_x86_ops->slot_disable_log_dirty = NULL;
7737 kvm_x86_ops->flush_log_dirty = NULL;
7738 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7739 }
7740
Yunhong Jiang64672c92016-06-13 14:19:59 -07007741 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7742 u64 vmx_msr;
7743
7744 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7745 cpu_preemption_timer_multi =
7746 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7747 } else {
7748 kvm_x86_ops->set_hv_timer = NULL;
7749 kvm_x86_ops->cancel_hv_timer = NULL;
7750 }
7751
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007752 if (!cpu_has_vmx_shadow_vmcs())
7753 enable_shadow_vmcs = 0;
7754 if (enable_shadow_vmcs)
7755 init_vmcs_shadow_fields();
7756
Feng Wubf9f6ac2015-09-18 22:29:55 +08007757 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007758 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007759
Ashok Rajc45dcc72016-06-22 14:59:56 +08007760 kvm_mce_cap_supported |= MCG_LMCE_P;
7761
Tiejun Chenf2c76482014-10-28 10:14:47 +08007762 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007763
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007764out:
Radim Krčmář23611332016-09-29 22:41:33 +02007765 for (i = 0; i < VMX_BITMAP_NR; i++)
7766 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007767
7768 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007769}
7770
7771static __exit void hardware_unsetup(void)
7772{
Radim Krčmář23611332016-09-29 22:41:33 +02007773 int i;
7774
7775 for (i = 0; i < VMX_BITMAP_NR; i++)
7776 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007777
Tiejun Chenf2c76482014-10-28 10:14:47 +08007778 free_kvm_area();
7779}
7780
Avi Kivity6aa8b732006-12-10 02:21:36 -08007781/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007782 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7783 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7784 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007785static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007786{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007787 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007788 grow_ple_window(vcpu);
7789
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007790 /*
7791 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7792 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7793 * never set PAUSE_EXITING and just set PLE if supported,
7794 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7795 */
7796 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007797 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007798}
7799
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007800static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007801{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007802 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007803}
7804
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007805static int handle_mwait(struct kvm_vcpu *vcpu)
7806{
7807 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7808 return handle_nop(vcpu);
7809}
7810
Jim Mattson45ec3682017-08-23 16:32:04 -07007811static int handle_invalid_op(struct kvm_vcpu *vcpu)
7812{
7813 kvm_queue_exception(vcpu, UD_VECTOR);
7814 return 1;
7815}
7816
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007817static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7818{
7819 return 1;
7820}
7821
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007822static int handle_monitor(struct kvm_vcpu *vcpu)
7823{
7824 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7825 return handle_nop(vcpu);
7826}
7827
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007828/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007829 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7830 * set the success or error code of an emulated VMX instruction, as specified
7831 * by Vol 2B, VMX Instruction Reference, "Conventions".
7832 */
7833static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7834{
7835 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7836 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7837 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7838}
7839
7840static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7841{
7842 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7843 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7844 X86_EFLAGS_SF | X86_EFLAGS_OF))
7845 | X86_EFLAGS_CF);
7846}
7847
Abel Gordon145c28d2013-04-18 14:36:55 +03007848static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007849 u32 vm_instruction_error)
7850{
7851 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7852 /*
7853 * failValid writes the error number to the current VMCS, which
7854 * can't be done there isn't a current VMCS.
7855 */
7856 nested_vmx_failInvalid(vcpu);
7857 return;
7858 }
7859 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7860 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7861 X86_EFLAGS_SF | X86_EFLAGS_OF))
7862 | X86_EFLAGS_ZF);
7863 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7864 /*
7865 * We don't need to force a shadow sync because
7866 * VM_INSTRUCTION_ERROR is not shadowed
7867 */
7868}
Abel Gordon145c28d2013-04-18 14:36:55 +03007869
Wincy Vanff651cb2014-12-11 08:52:58 +03007870static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7871{
7872 /* TODO: not to reset guest simply here. */
7873 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007874 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007875}
7876
Jan Kiszkaf4124502014-03-07 20:03:13 +01007877static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7878{
7879 struct vcpu_vmx *vmx =
7880 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7881
7882 vmx->nested.preemption_timer_expired = true;
7883 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7884 kvm_vcpu_kick(&vmx->vcpu);
7885
7886 return HRTIMER_NORESTART;
7887}
7888
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007889/*
Bandan Das19677e32014-05-06 02:19:15 -04007890 * Decode the memory-address operand of a vmx instruction, as recorded on an
7891 * exit caused by such an instruction (run by a guest hypervisor).
7892 * On success, returns 0. When the operand is invalid, returns 1 and throws
7893 * #UD or #GP.
7894 */
7895static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7896 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007897 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007898{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007899 gva_t off;
7900 bool exn;
7901 struct kvm_segment s;
7902
Bandan Das19677e32014-05-06 02:19:15 -04007903 /*
7904 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7905 * Execution", on an exit, vmx_instruction_info holds most of the
7906 * addressing components of the operand. Only the displacement part
7907 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7908 * For how an actual address is calculated from all these components,
7909 * refer to Vol. 1, "Operand Addressing".
7910 */
7911 int scaling = vmx_instruction_info & 3;
7912 int addr_size = (vmx_instruction_info >> 7) & 7;
7913 bool is_reg = vmx_instruction_info & (1u << 10);
7914 int seg_reg = (vmx_instruction_info >> 15) & 7;
7915 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7916 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7917 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7918 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7919
7920 if (is_reg) {
7921 kvm_queue_exception(vcpu, UD_VECTOR);
7922 return 1;
7923 }
7924
7925 /* Addr = segment_base + offset */
7926 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007927 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007928 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007929 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007930 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007931 off += kvm_register_read(vcpu, index_reg)<<scaling;
7932 vmx_get_segment(vcpu, &s, seg_reg);
7933 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007934
7935 if (addr_size == 1) /* 32 bit */
7936 *ret &= 0xffffffff;
7937
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007938 /* Checks for #GP/#SS exceptions. */
7939 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007940 if (is_long_mode(vcpu)) {
7941 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7942 * non-canonical form. This is the only check on the memory
7943 * destination for long mode!
7944 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007945 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007946 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007947 /* Protected mode: apply checks for segment validity in the
7948 * following order:
7949 * - segment type check (#GP(0) may be thrown)
7950 * - usability check (#GP(0)/#SS(0))
7951 * - limit check (#GP(0)/#SS(0))
7952 */
7953 if (wr)
7954 /* #GP(0) if the destination operand is located in a
7955 * read-only data segment or any code segment.
7956 */
7957 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7958 else
7959 /* #GP(0) if the source operand is located in an
7960 * execute-only code segment
7961 */
7962 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007963 if (exn) {
7964 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7965 return 1;
7966 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007967 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7968 */
7969 exn = (s.unusable != 0);
7970 /* Protected mode: #GP(0)/#SS(0) if the memory
7971 * operand is outside the segment limit.
7972 */
7973 exn = exn || (off + sizeof(u64) > s.limit);
7974 }
7975 if (exn) {
7976 kvm_queue_exception_e(vcpu,
7977 seg_reg == VCPU_SREG_SS ?
7978 SS_VECTOR : GP_VECTOR,
7979 0);
7980 return 1;
7981 }
7982
Bandan Das19677e32014-05-06 02:19:15 -04007983 return 0;
7984}
7985
Radim Krčmářcbf71272017-05-19 15:48:51 +02007986static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007987{
7988 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007989 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007990
7991 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007992 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007993 return 1;
7994
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007995 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007996 kvm_inject_page_fault(vcpu, &e);
7997 return 1;
7998 }
7999
Bandan Das3573e222014-05-06 02:19:16 -04008000 return 0;
8001}
8002
Liran Alonabfc52c2018-06-23 02:35:13 +03008003/*
8004 * Allocate a shadow VMCS and associate it with the currently loaded
8005 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8006 * VMCS is also VMCLEARed, so that it is ready for use.
8007 */
8008static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8009{
8010 struct vcpu_vmx *vmx = to_vmx(vcpu);
8011 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8012
8013 /*
8014 * We should allocate a shadow vmcs for vmcs01 only when L1
8015 * executes VMXON and free it when L1 executes VMXOFF.
8016 * As it is invalid to execute VMXON twice, we shouldn't reach
8017 * here when vmcs01 already have an allocated shadow vmcs.
8018 */
8019 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8020
8021 if (!loaded_vmcs->shadow_vmcs) {
8022 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8023 if (loaded_vmcs->shadow_vmcs)
8024 vmcs_clear(loaded_vmcs->shadow_vmcs);
8025 }
8026 return loaded_vmcs->shadow_vmcs;
8027}
8028
Jim Mattsone29acc52016-11-30 12:03:43 -08008029static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8030{
8031 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008032 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008033
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008034 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8035 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008036 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008037
8038 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8039 if (!vmx->nested.cached_vmcs12)
8040 goto out_cached_vmcs12;
8041
Liran Alon61ada742018-06-23 02:35:08 +03008042 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8043 if (!vmx->nested.cached_shadow_vmcs12)
8044 goto out_cached_shadow_vmcs12;
8045
Liran Alonabfc52c2018-06-23 02:35:13 +03008046 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8047 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008048
Jim Mattsone29acc52016-11-30 12:03:43 -08008049 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8050 HRTIMER_MODE_REL_PINNED);
8051 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8052
8053 vmx->nested.vmxon = true;
8054 return 0;
8055
8056out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008057 kfree(vmx->nested.cached_shadow_vmcs12);
8058
8059out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008060 kfree(vmx->nested.cached_vmcs12);
8061
8062out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008063 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008064
Jim Mattsonde3a0022017-11-27 17:22:25 -06008065out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008066 return -ENOMEM;
8067}
8068
Bandan Das3573e222014-05-06 02:19:16 -04008069/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008070 * Emulate the VMXON instruction.
8071 * Currently, we just remember that VMX is active, and do not save or even
8072 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8073 * do not currently need to store anything in that guest-allocated memory
8074 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8075 * argument is different from the VMXON pointer (which the spec says they do).
8076 */
8077static int handle_vmon(struct kvm_vcpu *vcpu)
8078{
Jim Mattsone29acc52016-11-30 12:03:43 -08008079 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008080 gpa_t vmptr;
8081 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008082 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008083 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8084 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008085
Jim Mattson70f3aac2017-04-26 08:53:46 -07008086 /*
8087 * The Intel VMX Instruction Reference lists a bunch of bits that are
8088 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8089 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8090 * Otherwise, we should fail with #UD. But most faulting conditions
8091 * have already been checked by hardware, prior to the VM-exit for
8092 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8093 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008094 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008095 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008096 kvm_queue_exception(vcpu, UD_VECTOR);
8097 return 1;
8098 }
8099
Felix Wilhelm727ba742018-06-11 09:43:44 +02008100 /* CPL=0 must be checked manually. */
8101 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008102 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008103 return 1;
8104 }
8105
Abel Gordon145c28d2013-04-18 14:36:55 +03008106 if (vmx->nested.vmxon) {
8107 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008108 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008109 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008110
Haozhong Zhang3b840802016-06-22 14:59:54 +08008111 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008112 != VMXON_NEEDED_FEATURES) {
8113 kvm_inject_gp(vcpu, 0);
8114 return 1;
8115 }
8116
Radim Krčmářcbf71272017-05-19 15:48:51 +02008117 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008118 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008119
8120 /*
8121 * SDM 3: 24.11.5
8122 * The first 4 bytes of VMXON region contain the supported
8123 * VMCS revision identifier
8124 *
8125 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8126 * which replaces physical address width with 32
8127 */
8128 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8129 nested_vmx_failInvalid(vcpu);
8130 return kvm_skip_emulated_instruction(vcpu);
8131 }
8132
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008133 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8134 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008135 nested_vmx_failInvalid(vcpu);
8136 return kvm_skip_emulated_instruction(vcpu);
8137 }
8138 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8139 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008140 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008141 nested_vmx_failInvalid(vcpu);
8142 return kvm_skip_emulated_instruction(vcpu);
8143 }
8144 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008145 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008146
8147 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008148 ret = enter_vmx_operation(vcpu);
8149 if (ret)
8150 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008151
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008152 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008153 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008154}
8155
8156/*
8157 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8158 * for running VMX instructions (except VMXON, whose prerequisites are
8159 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008160 * Note that many of these exceptions have priority over VM exits, so they
8161 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008162 */
8163static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8164{
Jim Mattsone49fcb82018-07-27 13:44:45 -07008165 if (!to_vmx(vcpu)->nested.vmxon) {
8166 kvm_queue_exception(vcpu, UD_VECTOR);
8167 return 0;
8168 }
8169
Felix Wilhelm727ba742018-06-11 09:43:44 +02008170 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008171 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008172 return 0;
8173 }
8174
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008175 return 1;
8176}
8177
David Matlack8ca44e82017-08-01 14:00:39 -07008178static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8179{
8180 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8181 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8182}
8183
Abel Gordone7953d72013-04-18 14:37:55 +03008184static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8185{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008186 if (vmx->nested.current_vmptr == -1ull)
8187 return;
8188
Abel Gordon012f83c2013-04-18 14:39:25 +03008189 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008190 /* copy to memory all shadowed fields in case
8191 they were modified */
8192 copy_shadow_to_vmcs12(vmx);
8193 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008194 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008195 }
Wincy Van705699a2015-02-03 23:58:17 +08008196 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008197
8198 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008199 kvm_vcpu_write_guest_page(&vmx->vcpu,
8200 vmx->nested.current_vmptr >> PAGE_SHIFT,
8201 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008202
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008203 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008204}
8205
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008206/*
8207 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8208 * just stops using VMX.
8209 */
8210static void free_nested(struct vcpu_vmx *vmx)
8211{
Wanpeng Lib7455822017-11-22 14:04:00 -08008212 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008213 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008214
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008215 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008216 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008217 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008218 vmx->nested.posted_intr_nv = -1;
8219 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008220 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008221 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008222 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8223 free_vmcs(vmx->vmcs01.shadow_vmcs);
8224 vmx->vmcs01.shadow_vmcs = NULL;
8225 }
David Matlack4f2777b2016-07-13 17:16:37 -07008226 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008227 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008228 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008229 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008230 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008231 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008232 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008233 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008234 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008235 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008236 }
Wincy Van705699a2015-02-03 23:58:17 +08008237 if (vmx->nested.pi_desc_page) {
8238 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008239 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008240 vmx->nested.pi_desc_page = NULL;
8241 vmx->nested.pi_desc = NULL;
8242 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008243
Jim Mattsonde3a0022017-11-27 17:22:25 -06008244 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008245}
8246
8247/* Emulate the VMXOFF instruction */
8248static int handle_vmoff(struct kvm_vcpu *vcpu)
8249{
8250 if (!nested_vmx_check_permission(vcpu))
8251 return 1;
8252 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008253 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008254 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008255}
8256
Nadav Har'El27d6c862011-05-25 23:06:59 +03008257/* Emulate the VMCLEAR instruction */
8258static int handle_vmclear(struct kvm_vcpu *vcpu)
8259{
8260 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008261 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008262 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008263
8264 if (!nested_vmx_check_permission(vcpu))
8265 return 1;
8266
Radim Krčmářcbf71272017-05-19 15:48:51 +02008267 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008268 return 1;
8269
Radim Krčmářcbf71272017-05-19 15:48:51 +02008270 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8271 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8272 return kvm_skip_emulated_instruction(vcpu);
8273 }
8274
8275 if (vmptr == vmx->nested.vmxon_ptr) {
8276 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8277 return kvm_skip_emulated_instruction(vcpu);
8278 }
8279
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008280 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008281 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008282
Jim Mattson587d7e722017-03-02 12:41:48 -08008283 kvm_vcpu_write_guest(vcpu,
8284 vmptr + offsetof(struct vmcs12, launch_state),
8285 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008286
Nadav Har'El27d6c862011-05-25 23:06:59 +03008287 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008288 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008289}
8290
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008291static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8292
8293/* Emulate the VMLAUNCH instruction */
8294static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8295{
8296 return nested_vmx_run(vcpu, true);
8297}
8298
8299/* Emulate the VMRESUME instruction */
8300static int handle_vmresume(struct kvm_vcpu *vcpu)
8301{
8302
8303 return nested_vmx_run(vcpu, false);
8304}
8305
Nadav Har'El49f705c2011-05-25 23:08:30 +03008306/*
8307 * Read a vmcs12 field. Since these can have varying lengths and we return
8308 * one type, we chose the biggest type (u64) and zero-extend the return value
8309 * to that size. Note that the caller, handle_vmread, might need to use only
8310 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8311 * 64-bit fields are to be returned).
8312 */
Liran Alone2536742018-06-23 02:35:02 +03008313static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008314 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008315{
8316 short offset = vmcs_field_to_offset(field);
8317 char *p;
8318
8319 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008320 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008321
Liran Alone2536742018-06-23 02:35:02 +03008322 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008323
Jim Mattsond37f4262017-12-22 12:12:16 -08008324 switch (vmcs_field_width(field)) {
8325 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008326 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008327 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008328 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008329 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008330 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008331 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008332 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008333 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008334 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008335 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008336 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008337 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008338 WARN_ON(1);
8339 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008340 }
8341}
8342
Abel Gordon20b97fe2013-04-18 14:36:25 +03008343
Liran Alone2536742018-06-23 02:35:02 +03008344static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008345 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008346 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008347 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008348 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008349 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008350
Jim Mattsond37f4262017-12-22 12:12:16 -08008351 switch (vmcs_field_width(field)) {
8352 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008353 *(u16 *)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_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008356 *(u32 *)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_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008359 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008360 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008361 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008362 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008363 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008364 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008365 WARN_ON(1);
8366 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008367 }
8368
8369}
8370
Jim Mattsonf4160e42018-05-29 09:11:33 -07008371/*
8372 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8373 * they have been modified by the L1 guest. Note that the "read-only"
8374 * VM-exit information fields are actually writable if the vCPU is
8375 * configured to support "VMWRITE to any supported field in the VMCS."
8376 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008377static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8378{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008379 const u16 *fields[] = {
8380 shadow_read_write_fields,
8381 shadow_read_only_fields
8382 };
8383 const int max_fields[] = {
8384 max_shadow_read_write_fields,
8385 max_shadow_read_only_fields
8386 };
8387 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008388 unsigned long field;
8389 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008390 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008391
Jan Kiszka282da872014-10-08 18:05:39 +02008392 preempt_disable();
8393
Abel Gordon16f5b902013-04-18 14:38:25 +03008394 vmcs_load(shadow_vmcs);
8395
Jim Mattsonf4160e42018-05-29 09:11:33 -07008396 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8397 for (i = 0; i < max_fields[q]; i++) {
8398 field = fields[q][i];
8399 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008400 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008401 }
8402 /*
8403 * Skip the VM-exit information fields if they are read-only.
8404 */
8405 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8406 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008407 }
8408
8409 vmcs_clear(shadow_vmcs);
8410 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008411
8412 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008413}
8414
Abel Gordonc3114422013-04-18 14:38:55 +03008415static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8416{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008417 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008418 shadow_read_write_fields,
8419 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008420 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008421 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008422 max_shadow_read_write_fields,
8423 max_shadow_read_only_fields
8424 };
8425 int i, q;
8426 unsigned long field;
8427 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008428 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008429
8430 vmcs_load(shadow_vmcs);
8431
Mathias Krausec2bae892013-06-26 20:36:21 +02008432 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008433 for (i = 0; i < max_fields[q]; i++) {
8434 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008435 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008436 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008437 }
8438 }
8439
8440 vmcs_clear(shadow_vmcs);
8441 vmcs_load(vmx->loaded_vmcs->vmcs);
8442}
8443
Nadav Har'El49f705c2011-05-25 23:08:30 +03008444/*
8445 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8446 * used before) all generate the same failure when it is missing.
8447 */
8448static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8449{
8450 struct vcpu_vmx *vmx = to_vmx(vcpu);
8451 if (vmx->nested.current_vmptr == -1ull) {
8452 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008453 return 0;
8454 }
8455 return 1;
8456}
8457
8458static int handle_vmread(struct kvm_vcpu *vcpu)
8459{
8460 unsigned long field;
8461 u64 field_value;
8462 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8463 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8464 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008465 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008466
Kyle Hueyeb277562016-11-29 12:40:39 -08008467 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008468 return 1;
8469
Kyle Huey6affcbe2016-11-29 12:40:40 -08008470 if (!nested_vmx_check_vmcs12(vcpu))
8471 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008472
Liran Alon6d894f42018-06-23 02:35:09 +03008473 if (!is_guest_mode(vcpu))
8474 vmcs12 = get_vmcs12(vcpu);
8475 else {
8476 /*
8477 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8478 * to shadowed-field sets the ALU flags for VMfailInvalid.
8479 */
8480 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8481 nested_vmx_failInvalid(vcpu);
8482 return kvm_skip_emulated_instruction(vcpu);
8483 }
8484 vmcs12 = get_shadow_vmcs12(vcpu);
8485 }
8486
Nadav Har'El49f705c2011-05-25 23:08:30 +03008487 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008488 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008489 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008490 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008491 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008492 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008493 }
8494 /*
8495 * Now copy part of this value to register or memory, as requested.
8496 * Note that the number of bits actually copied is 32 or 64 depending
8497 * on the guest's mode (32 or 64 bit), not on the given field's length.
8498 */
8499 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008500 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008501 field_value);
8502 } else {
8503 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008504 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008505 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008506 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008507 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8508 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008509 }
8510
8511 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008512 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008513}
8514
8515
8516static int handle_vmwrite(struct kvm_vcpu *vcpu)
8517{
8518 unsigned long field;
8519 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008520 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008521 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8522 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008523
Nadav Har'El49f705c2011-05-25 23:08:30 +03008524 /* The value to write might be 32 or 64 bits, depending on L1's long
8525 * mode, and eventually we need to write that into a field of several
8526 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008527 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008528 * bits into the vmcs12 field.
8529 */
8530 u64 field_value = 0;
8531 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008532 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008533
Kyle Hueyeb277562016-11-29 12:40:39 -08008534 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008535 return 1;
8536
Kyle Huey6affcbe2016-11-29 12:40:40 -08008537 if (!nested_vmx_check_vmcs12(vcpu))
8538 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008539
Nadav Har'El49f705c2011-05-25 23:08:30 +03008540 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008541 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008542 (((vmx_instruction_info) >> 3) & 0xf));
8543 else {
8544 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008545 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008546 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008547 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8548 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008549 kvm_inject_page_fault(vcpu, &e);
8550 return 1;
8551 }
8552 }
8553
8554
Nadav Amit27e6fb52014-06-18 17:19:26 +03008555 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008556 /*
8557 * If the vCPU supports "VMWRITE to any supported field in the
8558 * VMCS," then the "read-only" fields are actually read/write.
8559 */
8560 if (vmcs_field_readonly(field) &&
8561 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008562 nested_vmx_failValid(vcpu,
8563 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008564 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008565 }
8566
Liran Alon6d894f42018-06-23 02:35:09 +03008567 if (!is_guest_mode(vcpu))
8568 vmcs12 = get_vmcs12(vcpu);
8569 else {
8570 /*
8571 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8572 * to shadowed-field sets the ALU flags for VMfailInvalid.
8573 */
8574 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8575 nested_vmx_failInvalid(vcpu);
8576 return kvm_skip_emulated_instruction(vcpu);
8577 }
8578 vmcs12 = get_shadow_vmcs12(vcpu);
8579
8580 }
8581
8582 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008583 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008584 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008585 }
8586
Liran Alon6d894f42018-06-23 02:35:09 +03008587 /*
8588 * Do not track vmcs12 dirty-state if in guest-mode
8589 * as we actually dirty shadow vmcs12 instead of vmcs12.
8590 */
8591 if (!is_guest_mode(vcpu)) {
8592 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008593#define SHADOW_FIELD_RW(x) case x:
8594#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008595 /*
8596 * The fields that can be updated by L1 without a vmexit are
8597 * always updated in the vmcs02, the others go down the slow
8598 * path of prepare_vmcs02.
8599 */
8600 break;
8601 default:
8602 vmx->nested.dirty_vmcs12 = true;
8603 break;
8604 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008605 }
8606
Nadav Har'El49f705c2011-05-25 23:08:30 +03008607 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008608 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008609}
8610
Jim Mattsona8bc2842016-11-30 12:03:44 -08008611static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8612{
8613 vmx->nested.current_vmptr = vmptr;
8614 if (enable_shadow_vmcs) {
8615 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8616 SECONDARY_EXEC_SHADOW_VMCS);
8617 vmcs_write64(VMCS_LINK_POINTER,
8618 __pa(vmx->vmcs01.shadow_vmcs));
8619 vmx->nested.sync_shadow_vmcs = true;
8620 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008621 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008622}
8623
Nadav Har'El63846662011-05-25 23:07:29 +03008624/* Emulate the VMPTRLD instruction */
8625static int handle_vmptrld(struct kvm_vcpu *vcpu)
8626{
8627 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008628 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008629
8630 if (!nested_vmx_check_permission(vcpu))
8631 return 1;
8632
Radim Krčmářcbf71272017-05-19 15:48:51 +02008633 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008634 return 1;
8635
Radim Krčmářcbf71272017-05-19 15:48:51 +02008636 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8637 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8638 return kvm_skip_emulated_instruction(vcpu);
8639 }
8640
8641 if (vmptr == vmx->nested.vmxon_ptr) {
8642 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8643 return kvm_skip_emulated_instruction(vcpu);
8644 }
8645
Nadav Har'El63846662011-05-25 23:07:29 +03008646 if (vmx->nested.current_vmptr != vmptr) {
8647 struct vmcs12 *new_vmcs12;
8648 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008649 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8650 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008651 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008652 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008653 }
8654 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008655 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008656 (new_vmcs12->hdr.shadow_vmcs &&
8657 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008658 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008659 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008660 nested_vmx_failValid(vcpu,
8661 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008662 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008663 }
Nadav Har'El63846662011-05-25 23:07:29 +03008664
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008665 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008666 /*
8667 * Load VMCS12 from guest memory since it is not already
8668 * cached.
8669 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008670 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8671 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008672 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008673
Jim Mattsona8bc2842016-11-30 12:03:44 -08008674 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008675 }
8676
8677 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008678 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008679}
8680
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008681/* Emulate the VMPTRST instruction */
8682static int handle_vmptrst(struct kvm_vcpu *vcpu)
8683{
8684 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8685 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8686 gva_t vmcs_gva;
8687 struct x86_exception e;
8688
8689 if (!nested_vmx_check_permission(vcpu))
8690 return 1;
8691
8692 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008693 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008694 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008695 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008696 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8697 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8698 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008699 kvm_inject_page_fault(vcpu, &e);
8700 return 1;
8701 }
8702 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008703 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008704}
8705
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008706/* Emulate the INVEPT instruction */
8707static int handle_invept(struct kvm_vcpu *vcpu)
8708{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008709 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008710 u32 vmx_instruction_info, types;
8711 unsigned long type;
8712 gva_t gva;
8713 struct x86_exception e;
8714 struct {
8715 u64 eptp, gpa;
8716 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008717
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008718 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008719 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008720 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008721 kvm_queue_exception(vcpu, UD_VECTOR);
8722 return 1;
8723 }
8724
8725 if (!nested_vmx_check_permission(vcpu))
8726 return 1;
8727
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008728 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008729 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008730
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008731 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008732
Jim Mattson85c856b2016-10-26 08:38:38 -07008733 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008734 nested_vmx_failValid(vcpu,
8735 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008736 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008737 }
8738
8739 /* According to the Intel VMX instruction reference, the memory
8740 * operand is read even if it isn't needed (e.g., for type==global)
8741 */
8742 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008743 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008744 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008745 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008746 kvm_inject_page_fault(vcpu, &e);
8747 return 1;
8748 }
8749
8750 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008751 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008752 /*
8753 * TODO: track mappings and invalidate
8754 * single context requests appropriately
8755 */
8756 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008757 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008758 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008759 nested_vmx_succeed(vcpu);
8760 break;
8761 default:
8762 BUG_ON(1);
8763 break;
8764 }
8765
Kyle Huey6affcbe2016-11-29 12:40:40 -08008766 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008767}
8768
Petr Matouseka642fc32014-09-23 20:22:30 +02008769static int handle_invvpid(struct kvm_vcpu *vcpu)
8770{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008771 struct vcpu_vmx *vmx = to_vmx(vcpu);
8772 u32 vmx_instruction_info;
8773 unsigned long type, types;
8774 gva_t gva;
8775 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008776 struct {
8777 u64 vpid;
8778 u64 gla;
8779 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008780
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008781 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008782 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008783 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008784 kvm_queue_exception(vcpu, UD_VECTOR);
8785 return 1;
8786 }
8787
8788 if (!nested_vmx_check_permission(vcpu))
8789 return 1;
8790
8791 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8792 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8793
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008794 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008795 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008796
Jim Mattson85c856b2016-10-26 08:38:38 -07008797 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008798 nested_vmx_failValid(vcpu,
8799 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008800 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008801 }
8802
8803 /* according to the intel vmx instruction reference, the memory
8804 * operand is read even if it isn't needed (e.g., for type==global)
8805 */
8806 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8807 vmx_instruction_info, false, &gva))
8808 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008809 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008810 kvm_inject_page_fault(vcpu, &e);
8811 return 1;
8812 }
Jim Mattson40352602017-06-28 09:37:37 -07008813 if (operand.vpid >> 16) {
8814 nested_vmx_failValid(vcpu,
8815 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8816 return kvm_skip_emulated_instruction(vcpu);
8817 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008818
8819 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008820 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008821 if (!operand.vpid ||
8822 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008823 nested_vmx_failValid(vcpu,
8824 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8825 return kvm_skip_emulated_instruction(vcpu);
8826 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008827 if (cpu_has_vmx_invvpid_individual_addr() &&
8828 vmx->nested.vpid02) {
8829 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8830 vmx->nested.vpid02, operand.gla);
8831 } else
8832 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8833 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008834 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008835 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008836 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008837 nested_vmx_failValid(vcpu,
8838 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008839 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008840 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008841 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008842 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008843 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008844 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008845 break;
8846 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008847 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008848 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008849 }
8850
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008851 nested_vmx_succeed(vcpu);
8852
Kyle Huey6affcbe2016-11-29 12:40:40 -08008853 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008854}
8855
Junaid Shahideb4b2482018-06-27 14:59:14 -07008856static int handle_invpcid(struct kvm_vcpu *vcpu)
8857{
8858 u32 vmx_instruction_info;
8859 unsigned long type;
8860 bool pcid_enabled;
8861 gva_t gva;
8862 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07008863 unsigned i;
8864 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07008865 struct {
8866 u64 pcid;
8867 u64 gla;
8868 } operand;
8869
8870 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
8871 kvm_queue_exception(vcpu, UD_VECTOR);
8872 return 1;
8873 }
8874
8875 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8876 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8877
8878 if (type > 3) {
8879 kvm_inject_gp(vcpu, 0);
8880 return 1;
8881 }
8882
8883 /* According to the Intel instruction reference, the memory operand
8884 * is read even if it isn't needed (e.g., for type==all)
8885 */
8886 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8887 vmx_instruction_info, false, &gva))
8888 return 1;
8889
8890 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8891 kvm_inject_page_fault(vcpu, &e);
8892 return 1;
8893 }
8894
8895 if (operand.pcid >> 12 != 0) {
8896 kvm_inject_gp(vcpu, 0);
8897 return 1;
8898 }
8899
8900 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
8901
8902 switch (type) {
8903 case INVPCID_TYPE_INDIV_ADDR:
8904 if ((!pcid_enabled && (operand.pcid != 0)) ||
8905 is_noncanonical_address(operand.gla, vcpu)) {
8906 kvm_inject_gp(vcpu, 0);
8907 return 1;
8908 }
8909 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
8910 return kvm_skip_emulated_instruction(vcpu);
8911
8912 case INVPCID_TYPE_SINGLE_CTXT:
8913 if (!pcid_enabled && (operand.pcid != 0)) {
8914 kvm_inject_gp(vcpu, 0);
8915 return 1;
8916 }
8917
8918 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
8919 kvm_mmu_sync_roots(vcpu);
8920 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8921 }
8922
Junaid Shahidb94742c2018-06-27 14:59:20 -07008923 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
8924 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
8925 == operand.pcid)
8926 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07008927
Junaid Shahidb94742c2018-06-27 14:59:20 -07008928 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07008929 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07008930 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07008931 * given PCID, then nothing needs to be done here because a
8932 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07008933 */
8934
8935 return kvm_skip_emulated_instruction(vcpu);
8936
8937 case INVPCID_TYPE_ALL_NON_GLOBAL:
8938 /*
8939 * Currently, KVM doesn't mark global entries in the shadow
8940 * page tables, so a non-global flush just degenerates to a
8941 * global flush. If needed, we could optimize this later by
8942 * keeping track of global entries in shadow page tables.
8943 */
8944
8945 /* fall-through */
8946 case INVPCID_TYPE_ALL_INCL_GLOBAL:
8947 kvm_mmu_unload(vcpu);
8948 return kvm_skip_emulated_instruction(vcpu);
8949
8950 default:
8951 BUG(); /* We have already checked above that type <= 3 */
8952 }
8953}
8954
Kai Huang843e4332015-01-28 10:54:28 +08008955static int handle_pml_full(struct kvm_vcpu *vcpu)
8956{
8957 unsigned long exit_qualification;
8958
8959 trace_kvm_pml_full(vcpu->vcpu_id);
8960
8961 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8962
8963 /*
8964 * PML buffer FULL happened while executing iret from NMI,
8965 * "blocked by NMI" bit has to be set before next VM entry.
8966 */
8967 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008968 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008969 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8970 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8971 GUEST_INTR_STATE_NMI);
8972
8973 /*
8974 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8975 * here.., and there's no userspace involvement needed for PML.
8976 */
8977 return 1;
8978}
8979
Yunhong Jiang64672c92016-06-13 14:19:59 -07008980static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8981{
8982 kvm_lapic_expired_hv_timer(vcpu);
8983 return 1;
8984}
8985
Bandan Das41ab9372017-08-03 15:54:43 -04008986static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8987{
8988 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008989 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8990
8991 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008992 switch (address & VMX_EPTP_MT_MASK) {
8993 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008994 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008995 return false;
8996 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008997 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008998 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008999 return false;
9000 break;
9001 default:
9002 return false;
9003 }
9004
David Hildenbrandbb97a012017-08-10 23:15:28 +02009005 /* only 4 levels page-walk length are valid */
9006 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009007 return false;
9008
9009 /* Reserved bits should not be set */
9010 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9011 return false;
9012
9013 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009014 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009015 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009016 return false;
9017 }
9018
9019 return true;
9020}
9021
9022static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9023 struct vmcs12 *vmcs12)
9024{
9025 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9026 u64 address;
9027 bool accessed_dirty;
9028 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9029
9030 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9031 !nested_cpu_has_ept(vmcs12))
9032 return 1;
9033
9034 if (index >= VMFUNC_EPTP_ENTRIES)
9035 return 1;
9036
9037
9038 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9039 &address, index * 8, 8))
9040 return 1;
9041
David Hildenbrandbb97a012017-08-10 23:15:28 +02009042 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009043
9044 /*
9045 * If the (L2) guest does a vmfunc to the currently
9046 * active ept pointer, we don't have to do anything else
9047 */
9048 if (vmcs12->ept_pointer != address) {
9049 if (!valid_ept_address(vcpu, address))
9050 return 1;
9051
9052 kvm_mmu_unload(vcpu);
9053 mmu->ept_ad = accessed_dirty;
9054 mmu->base_role.ad_disabled = !accessed_dirty;
9055 vmcs12->ept_pointer = address;
9056 /*
9057 * TODO: Check what's the correct approach in case
9058 * mmu reload fails. Currently, we just let the next
9059 * reload potentially fail
9060 */
9061 kvm_mmu_reload(vcpu);
9062 }
9063
9064 return 0;
9065}
9066
Bandan Das2a499e42017-08-03 15:54:41 -04009067static int handle_vmfunc(struct kvm_vcpu *vcpu)
9068{
Bandan Das27c42a12017-08-03 15:54:42 -04009069 struct vcpu_vmx *vmx = to_vmx(vcpu);
9070 struct vmcs12 *vmcs12;
9071 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9072
9073 /*
9074 * VMFUNC is only supported for nested guests, but we always enable the
9075 * secondary control for simplicity; for non-nested mode, fake that we
9076 * didn't by injecting #UD.
9077 */
9078 if (!is_guest_mode(vcpu)) {
9079 kvm_queue_exception(vcpu, UD_VECTOR);
9080 return 1;
9081 }
9082
9083 vmcs12 = get_vmcs12(vcpu);
9084 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9085 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009086
9087 switch (function) {
9088 case 0:
9089 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9090 goto fail;
9091 break;
9092 default:
9093 goto fail;
9094 }
9095 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009096
9097fail:
9098 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9099 vmcs_read32(VM_EXIT_INTR_INFO),
9100 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009101 return 1;
9102}
9103
Nadav Har'El0140cae2011-05-25 23:06:28 +03009104/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009105 * The exit handlers return 1 if the exit was handled fully and guest execution
9106 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9107 * to be done to userspace and return 0.
9108 */
Mathias Krause772e0312012-08-30 01:30:19 +02009109static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009110 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9111 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009112 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009113 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009114 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009115 [EXIT_REASON_CR_ACCESS] = handle_cr,
9116 [EXIT_REASON_DR_ACCESS] = handle_dr,
9117 [EXIT_REASON_CPUID] = handle_cpuid,
9118 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9119 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9120 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9121 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009122 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009123 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009124 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009125 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009126 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009127 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009128 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009129 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009130 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009131 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009132 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009133 [EXIT_REASON_VMOFF] = handle_vmoff,
9134 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009135 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9136 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009137 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009138 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009139 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009140 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009141 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009142 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009143 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9144 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009145 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9146 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009147 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009148 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009149 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009150 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009151 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009152 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009153 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009154 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009155 [EXIT_REASON_XSAVES] = handle_xsaves,
9156 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009157 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009158 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009159 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009160 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009161};
9162
9163static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009164 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009165
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009166static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9167 struct vmcs12 *vmcs12)
9168{
9169 unsigned long exit_qualification;
9170 gpa_t bitmap, last_bitmap;
9171 unsigned int port;
9172 int size;
9173 u8 b;
9174
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009175 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009176 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009177
9178 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9179
9180 port = exit_qualification >> 16;
9181 size = (exit_qualification & 7) + 1;
9182
9183 last_bitmap = (gpa_t)-1;
9184 b = -1;
9185
9186 while (size > 0) {
9187 if (port < 0x8000)
9188 bitmap = vmcs12->io_bitmap_a;
9189 else if (port < 0x10000)
9190 bitmap = vmcs12->io_bitmap_b;
9191 else
Joe Perches1d804d02015-03-30 16:46:09 -07009192 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009193 bitmap += (port & 0x7fff) / 8;
9194
9195 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009196 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009197 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009198 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009199 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009200
9201 port++;
9202 size--;
9203 last_bitmap = bitmap;
9204 }
9205
Joe Perches1d804d02015-03-30 16:46:09 -07009206 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009207}
9208
Nadav Har'El644d7112011-05-25 23:12:35 +03009209/*
9210 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9211 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9212 * disinterest in the current event (read or write a specific MSR) by using an
9213 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9214 */
9215static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9216 struct vmcs12 *vmcs12, u32 exit_reason)
9217{
9218 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9219 gpa_t bitmap;
9220
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009221 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009222 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009223
9224 /*
9225 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9226 * for the four combinations of read/write and low/high MSR numbers.
9227 * First we need to figure out which of the four to use:
9228 */
9229 bitmap = vmcs12->msr_bitmap;
9230 if (exit_reason == EXIT_REASON_MSR_WRITE)
9231 bitmap += 2048;
9232 if (msr_index >= 0xc0000000) {
9233 msr_index -= 0xc0000000;
9234 bitmap += 1024;
9235 }
9236
9237 /* Then read the msr_index'th bit from this bitmap: */
9238 if (msr_index < 1024*8) {
9239 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009240 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009241 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009242 return 1 & (b >> (msr_index & 7));
9243 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009244 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009245}
9246
9247/*
9248 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9249 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9250 * intercept (via guest_host_mask etc.) the current event.
9251 */
9252static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9253 struct vmcs12 *vmcs12)
9254{
9255 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9256 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009257 int reg;
9258 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009259
9260 switch ((exit_qualification >> 4) & 3) {
9261 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009262 reg = (exit_qualification >> 8) & 15;
9263 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009264 switch (cr) {
9265 case 0:
9266 if (vmcs12->cr0_guest_host_mask &
9267 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009268 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009269 break;
9270 case 3:
9271 if ((vmcs12->cr3_target_count >= 1 &&
9272 vmcs12->cr3_target_value0 == val) ||
9273 (vmcs12->cr3_target_count >= 2 &&
9274 vmcs12->cr3_target_value1 == val) ||
9275 (vmcs12->cr3_target_count >= 3 &&
9276 vmcs12->cr3_target_value2 == val) ||
9277 (vmcs12->cr3_target_count >= 4 &&
9278 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009279 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009280 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009281 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009282 break;
9283 case 4:
9284 if (vmcs12->cr4_guest_host_mask &
9285 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009286 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009287 break;
9288 case 8:
9289 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009290 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009291 break;
9292 }
9293 break;
9294 case 2: /* clts */
9295 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9296 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009297 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009298 break;
9299 case 1: /* mov from cr */
9300 switch (cr) {
9301 case 3:
9302 if (vmcs12->cpu_based_vm_exec_control &
9303 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009304 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009305 break;
9306 case 8:
9307 if (vmcs12->cpu_based_vm_exec_control &
9308 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009309 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009310 break;
9311 }
9312 break;
9313 case 3: /* lmsw */
9314 /*
9315 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9316 * cr0. Other attempted changes are ignored, with no exit.
9317 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009318 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009319 if (vmcs12->cr0_guest_host_mask & 0xe &
9320 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009321 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009322 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9323 !(vmcs12->cr0_read_shadow & 0x1) &&
9324 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009325 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009326 break;
9327 }
Joe Perches1d804d02015-03-30 16:46:09 -07009328 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009329}
9330
Liran Alona7cde482018-06-23 02:35:10 +03009331static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9332 struct vmcs12 *vmcs12, gpa_t bitmap)
9333{
9334 u32 vmx_instruction_info;
9335 unsigned long field;
9336 u8 b;
9337
9338 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9339 return true;
9340
9341 /* Decode instruction info and find the field to access */
9342 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9343 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9344
9345 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9346 if (field >> 15)
9347 return true;
9348
9349 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9350 return true;
9351
9352 return 1 & (b >> (field & 7));
9353}
9354
Nadav Har'El644d7112011-05-25 23:12:35 +03009355/*
9356 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9357 * should handle it ourselves in L0 (and then continue L2). Only call this
9358 * when in is_guest_mode (L2).
9359 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009360static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009361{
Nadav Har'El644d7112011-05-25 23:12:35 +03009362 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9363 struct vcpu_vmx *vmx = to_vmx(vcpu);
9364 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9365
Jim Mattson4f350c62017-09-14 16:31:44 -07009366 if (vmx->nested.nested_run_pending)
9367 return false;
9368
9369 if (unlikely(vmx->fail)) {
9370 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9371 vmcs_read32(VM_INSTRUCTION_ERROR));
9372 return true;
9373 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009374
David Matlackc9f04402017-08-01 14:00:40 -07009375 /*
9376 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009377 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9378 * Page). The CPU may write to these pages via their host
9379 * physical address while L2 is running, bypassing any
9380 * address-translation-based dirty tracking (e.g. EPT write
9381 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009382 *
9383 * Mark them dirty on every exit from L2 to prevent them from
9384 * getting out of sync with dirty tracking.
9385 */
9386 nested_mark_vmcs12_pages_dirty(vcpu);
9387
Jim Mattson4f350c62017-09-14 16:31:44 -07009388 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9389 vmcs_readl(EXIT_QUALIFICATION),
9390 vmx->idt_vectoring_info,
9391 intr_info,
9392 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9393 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009394
9395 switch (exit_reason) {
9396 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009397 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009398 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009399 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009400 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009401 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009402 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009403 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009404 else if (is_debug(intr_info) &&
9405 vcpu->guest_debug &
9406 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9407 return false;
9408 else if (is_breakpoint(intr_info) &&
9409 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9410 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009411 return vmcs12->exception_bitmap &
9412 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9413 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009414 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009415 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009416 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009417 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009418 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009419 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009420 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009421 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009422 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009423 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009424 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009425 case EXIT_REASON_HLT:
9426 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9427 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009428 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009429 case EXIT_REASON_INVLPG:
9430 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9431 case EXIT_REASON_RDPMC:
9432 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009433 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009434 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009435 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009436 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009437 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009438 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009439 case EXIT_REASON_VMREAD:
9440 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9441 vmcs12->vmread_bitmap);
9442 case EXIT_REASON_VMWRITE:
9443 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9444 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009445 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9446 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009447 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009448 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009449 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009450 /*
9451 * VMX instructions trap unconditionally. This allows L1 to
9452 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9453 */
Joe Perches1d804d02015-03-30 16:46:09 -07009454 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009455 case EXIT_REASON_CR_ACCESS:
9456 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9457 case EXIT_REASON_DR_ACCESS:
9458 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9459 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009460 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009461 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9462 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009463 case EXIT_REASON_MSR_READ:
9464 case EXIT_REASON_MSR_WRITE:
9465 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9466 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009467 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009468 case EXIT_REASON_MWAIT_INSTRUCTION:
9469 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009470 case EXIT_REASON_MONITOR_TRAP_FLAG:
9471 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009472 case EXIT_REASON_MONITOR_INSTRUCTION:
9473 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9474 case EXIT_REASON_PAUSE_INSTRUCTION:
9475 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9476 nested_cpu_has2(vmcs12,
9477 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9478 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009479 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009480 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009481 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009482 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009483 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009484 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009485 /*
9486 * The controls for "virtualize APIC accesses," "APIC-
9487 * register virtualization," and "virtual-interrupt
9488 * delivery" only come from vmcs12.
9489 */
Joe Perches1d804d02015-03-30 16:46:09 -07009490 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009491 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009492 /*
9493 * L0 always deals with the EPT violation. If nested EPT is
9494 * used, and the nested mmu code discovers that the address is
9495 * missing in the guest EPT table (EPT12), the EPT violation
9496 * will be injected with nested_ept_inject_page_fault()
9497 */
Joe Perches1d804d02015-03-30 16:46:09 -07009498 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009499 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009500 /*
9501 * L2 never uses directly L1's EPT, but rather L0's own EPT
9502 * table (shadow on EPT) or a merged EPT table that L0 built
9503 * (EPT on EPT). So any problems with the structure of the
9504 * table is L0's fault.
9505 */
Joe Perches1d804d02015-03-30 16:46:09 -07009506 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009507 case EXIT_REASON_INVPCID:
9508 return
9509 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9510 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009511 case EXIT_REASON_WBINVD:
9512 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9513 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009514 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009515 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9516 /*
9517 * This should never happen, since it is not possible to
9518 * set XSS to a non-zero value---neither in L1 nor in L2.
9519 * If if it were, XSS would have to be checked against
9520 * the XSS exit bitmap in vmcs12.
9521 */
9522 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009523 case EXIT_REASON_PREEMPTION_TIMER:
9524 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009525 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009526 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009527 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009528 case EXIT_REASON_VMFUNC:
9529 /* VM functions are emulated through L2->L0 vmexits. */
9530 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009531 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009532 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009533 }
9534}
9535
Paolo Bonzini7313c692017-07-27 10:31:25 +02009536static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9537{
9538 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9539
9540 /*
9541 * At this point, the exit interruption info in exit_intr_info
9542 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9543 * we need to query the in-kernel LAPIC.
9544 */
9545 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9546 if ((exit_intr_info &
9547 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9548 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9549 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9550 vmcs12->vm_exit_intr_error_code =
9551 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9552 }
9553
9554 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9555 vmcs_readl(EXIT_QUALIFICATION));
9556 return 1;
9557}
9558
Avi Kivity586f9602010-11-18 13:09:54 +02009559static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9560{
9561 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9562 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9563}
9564
Kai Huanga3eaa862015-11-04 13:46:05 +08009565static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009566{
Kai Huanga3eaa862015-11-04 13:46:05 +08009567 if (vmx->pml_pg) {
9568 __free_page(vmx->pml_pg);
9569 vmx->pml_pg = NULL;
9570 }
Kai Huang843e4332015-01-28 10:54:28 +08009571}
9572
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009573static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009574{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009575 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009576 u64 *pml_buf;
9577 u16 pml_idx;
9578
9579 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9580
9581 /* Do nothing if PML buffer is empty */
9582 if (pml_idx == (PML_ENTITY_NUM - 1))
9583 return;
9584
9585 /* PML index always points to next available PML buffer entity */
9586 if (pml_idx >= PML_ENTITY_NUM)
9587 pml_idx = 0;
9588 else
9589 pml_idx++;
9590
9591 pml_buf = page_address(vmx->pml_pg);
9592 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9593 u64 gpa;
9594
9595 gpa = pml_buf[pml_idx];
9596 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009597 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009598 }
9599
9600 /* reset PML index */
9601 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9602}
9603
9604/*
9605 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9606 * Called before reporting dirty_bitmap to userspace.
9607 */
9608static void kvm_flush_pml_buffers(struct kvm *kvm)
9609{
9610 int i;
9611 struct kvm_vcpu *vcpu;
9612 /*
9613 * We only need to kick vcpu out of guest mode here, as PML buffer
9614 * is flushed at beginning of all VMEXITs, and it's obvious that only
9615 * vcpus running in guest are possible to have unflushed GPAs in PML
9616 * buffer.
9617 */
9618 kvm_for_each_vcpu(i, vcpu, kvm)
9619 kvm_vcpu_kick(vcpu);
9620}
9621
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009622static void vmx_dump_sel(char *name, uint32_t sel)
9623{
9624 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009625 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009626 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9627 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9628 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9629}
9630
9631static void vmx_dump_dtsel(char *name, uint32_t limit)
9632{
9633 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9634 name, vmcs_read32(limit),
9635 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9636}
9637
9638static void dump_vmcs(void)
9639{
9640 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9641 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9642 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9643 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9644 u32 secondary_exec_control = 0;
9645 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009646 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009647 int i, n;
9648
9649 if (cpu_has_secondary_exec_ctrls())
9650 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9651
9652 pr_err("*** Guest State ***\n");
9653 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9654 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9655 vmcs_readl(CR0_GUEST_HOST_MASK));
9656 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9657 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9658 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9659 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9660 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9661 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009662 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9663 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9664 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9665 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009666 }
9667 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9668 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9669 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9670 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9671 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9672 vmcs_readl(GUEST_SYSENTER_ESP),
9673 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9674 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9675 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9676 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9677 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9678 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9679 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9680 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9681 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9682 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9683 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9684 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9685 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009686 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9687 efer, vmcs_read64(GUEST_IA32_PAT));
9688 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9689 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009690 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009691 if (cpu_has_load_perf_global_ctrl &&
9692 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009693 pr_err("PerfGlobCtl = 0x%016llx\n",
9694 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009695 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009696 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009697 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9698 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9699 vmcs_read32(GUEST_ACTIVITY_STATE));
9700 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9701 pr_err("InterruptStatus = %04x\n",
9702 vmcs_read16(GUEST_INTR_STATUS));
9703
9704 pr_err("*** Host State ***\n");
9705 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9706 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9707 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9708 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9709 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9710 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9711 vmcs_read16(HOST_TR_SELECTOR));
9712 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9713 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9714 vmcs_readl(HOST_TR_BASE));
9715 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9716 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9717 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9718 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9719 vmcs_readl(HOST_CR4));
9720 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9721 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9722 vmcs_read32(HOST_IA32_SYSENTER_CS),
9723 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9724 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009725 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9726 vmcs_read64(HOST_IA32_EFER),
9727 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009728 if (cpu_has_load_perf_global_ctrl &&
9729 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009730 pr_err("PerfGlobCtl = 0x%016llx\n",
9731 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009732
9733 pr_err("*** Control State ***\n");
9734 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9735 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9736 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9737 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9738 vmcs_read32(EXCEPTION_BITMAP),
9739 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9740 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9741 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9742 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9743 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9744 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9745 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9746 vmcs_read32(VM_EXIT_INTR_INFO),
9747 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9748 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9749 pr_err(" reason=%08x qualification=%016lx\n",
9750 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9751 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9752 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9753 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009754 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009755 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009756 pr_err("TSC Multiplier = 0x%016llx\n",
9757 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009758 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9759 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9760 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9761 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9762 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009763 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009764 n = vmcs_read32(CR3_TARGET_COUNT);
9765 for (i = 0; i + 1 < n; i += 4)
9766 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9767 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9768 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9769 if (i < n)
9770 pr_err("CR3 target%u=%016lx\n",
9771 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9772 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9773 pr_err("PLE Gap=%08x Window=%08x\n",
9774 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9775 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9776 pr_err("Virtual processor ID = 0x%04x\n",
9777 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9778}
9779
Avi Kivity6aa8b732006-12-10 02:21:36 -08009780/*
9781 * The guest has exited. See if we can fix it or if we need userspace
9782 * assistance.
9783 */
Avi Kivity851ba692009-08-24 11:10:17 +03009784static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009785{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009786 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009787 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009788 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009789
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009790 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9791
Kai Huang843e4332015-01-28 10:54:28 +08009792 /*
9793 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9794 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9795 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9796 * mode as if vcpus is in root mode, the PML buffer must has been
9797 * flushed already.
9798 */
9799 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009800 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009801
Mohammed Gamal80ced182009-09-01 12:48:18 +02009802 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009803 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009804 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009805
Paolo Bonzini7313c692017-07-27 10:31:25 +02009806 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9807 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009808
Mohammed Gamal51207022010-05-31 22:40:54 +03009809 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009810 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009811 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9812 vcpu->run->fail_entry.hardware_entry_failure_reason
9813 = exit_reason;
9814 return 0;
9815 }
9816
Avi Kivity29bd8a72007-09-10 17:27:03 +03009817 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009818 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9819 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009820 = vmcs_read32(VM_INSTRUCTION_ERROR);
9821 return 0;
9822 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009823
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009824 /*
9825 * Note:
9826 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9827 * delivery event since it indicates guest is accessing MMIO.
9828 * The vm-exit can be triggered again after return to guest that
9829 * will cause infinite loop.
9830 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009831 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009832 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009833 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009834 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009835 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9836 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9837 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009838 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009839 vcpu->run->internal.data[0] = vectoring_info;
9840 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009841 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9842 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9843 vcpu->run->internal.ndata++;
9844 vcpu->run->internal.data[3] =
9845 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9846 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009847 return 0;
9848 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009849
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009850 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009851 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9852 if (vmx_interrupt_allowed(vcpu)) {
9853 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9854 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9855 vcpu->arch.nmi_pending) {
9856 /*
9857 * This CPU don't support us in finding the end of an
9858 * NMI-blocked window if the guest runs with IRQs
9859 * disabled. So we pull the trigger after 1 s of
9860 * futile waiting, but inform the user about this.
9861 */
9862 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9863 "state on VCPU %d after 1 s timeout\n",
9864 __func__, vcpu->vcpu_id);
9865 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9866 }
9867 }
9868
Avi Kivity6aa8b732006-12-10 02:21:36 -08009869 if (exit_reason < kvm_vmx_max_exit_handlers
9870 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009871 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009872 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009873 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9874 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009875 kvm_queue_exception(vcpu, UD_VECTOR);
9876 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009877 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009878}
9879
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009880static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009881{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009882 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9883
9884 if (is_guest_mode(vcpu) &&
9885 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9886 return;
9887
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009888 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009889 vmcs_write32(TPR_THRESHOLD, 0);
9890 return;
9891 }
9892
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009893 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009894}
9895
Jim Mattson8d860bb2018-05-09 16:56:05 -04009896static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009897{
9898 u32 sec_exec_control;
9899
Jim Mattson8d860bb2018-05-09 16:56:05 -04009900 if (!lapic_in_kernel(vcpu))
9901 return;
9902
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009903 /* Postpone execution until vmcs01 is the current VMCS. */
9904 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009905 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009906 return;
9907 }
9908
Paolo Bonzini35754c92015-07-29 12:05:37 +02009909 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009910 return;
9911
9912 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009913 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9914 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009915
Jim Mattson8d860bb2018-05-09 16:56:05 -04009916 switch (kvm_get_apic_mode(vcpu)) {
9917 case LAPIC_MODE_INVALID:
9918 WARN_ONCE(true, "Invalid local APIC state");
9919 case LAPIC_MODE_DISABLED:
9920 break;
9921 case LAPIC_MODE_XAPIC:
9922 if (flexpriority_enabled) {
9923 sec_exec_control |=
9924 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9925 vmx_flush_tlb(vcpu, true);
9926 }
9927 break;
9928 case LAPIC_MODE_X2APIC:
9929 if (cpu_has_vmx_virtualize_x2apic_mode())
9930 sec_exec_control |=
9931 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9932 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009933 }
9934 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9935
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009936 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009937}
9938
Tang Chen38b99172014-09-24 15:57:54 +08009939static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9940{
Jim Mattsonab5df312018-05-09 17:02:03 -04009941 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009942 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009943 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009944 }
Tang Chen38b99172014-09-24 15:57:54 +08009945}
9946
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009947static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009948{
9949 u16 status;
9950 u8 old;
9951
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009952 if (max_isr == -1)
9953 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009954
9955 status = vmcs_read16(GUEST_INTR_STATUS);
9956 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009957 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009958 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009959 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009960 vmcs_write16(GUEST_INTR_STATUS, status);
9961 }
9962}
9963
9964static void vmx_set_rvi(int vector)
9965{
9966 u16 status;
9967 u8 old;
9968
Wei Wang4114c272014-11-05 10:53:43 +08009969 if (vector == -1)
9970 vector = 0;
9971
Yang Zhangc7c9c562013-01-25 10:18:51 +08009972 status = vmcs_read16(GUEST_INTR_STATUS);
9973 old = (u8)status & 0xff;
9974 if ((u8)vector != old) {
9975 status &= ~0xff;
9976 status |= (u8)vector;
9977 vmcs_write16(GUEST_INTR_STATUS, status);
9978 }
9979}
9980
9981static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9982{
Liran Alon851c1a182017-12-24 18:12:56 +02009983 /*
9984 * When running L2, updating RVI is only relevant when
9985 * vmcs12 virtual-interrupt-delivery enabled.
9986 * However, it can be enabled only when L1 also
9987 * intercepts external-interrupts and in that case
9988 * we should not update vmcs02 RVI but instead intercept
9989 * interrupt. Therefore, do nothing when running L2.
9990 */
9991 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009992 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009993}
9994
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009995static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009996{
9997 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009998 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009999 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010000
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010001 WARN_ON(!vcpu->arch.apicv_active);
10002 if (pi_test_on(&vmx->pi_desc)) {
10003 pi_clear_on(&vmx->pi_desc);
10004 /*
10005 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10006 * But on x86 this is just a compiler barrier anyway.
10007 */
10008 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010009 max_irr_updated =
10010 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10011
10012 /*
10013 * If we are running L2 and L1 has a new pending interrupt
10014 * which can be injected, we should re-evaluate
10015 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010016 * If L1 intercepts external-interrupts, we should
10017 * exit from L2 to L1. Otherwise, interrupt should be
10018 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010019 */
Liran Alon851c1a182017-12-24 18:12:56 +020010020 if (is_guest_mode(vcpu) && max_irr_updated) {
10021 if (nested_exit_on_intr(vcpu))
10022 kvm_vcpu_exiting_guest_mode(vcpu);
10023 else
10024 kvm_make_request(KVM_REQ_EVENT, vcpu);
10025 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010026 } else {
10027 max_irr = kvm_lapic_find_highest_irr(vcpu);
10028 }
10029 vmx_hwapic_irr_update(vcpu, max_irr);
10030 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010031}
10032
Andrey Smetanin63086302015-11-10 15:36:32 +030010033static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010034{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010035 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010036 return;
10037
Yang Zhangc7c9c562013-01-25 10:18:51 +080010038 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10039 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10040 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10041 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10042}
10043
Paolo Bonzini967235d2016-12-19 14:03:45 +010010044static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10045{
10046 struct vcpu_vmx *vmx = to_vmx(vcpu);
10047
10048 pi_clear_on(&vmx->pi_desc);
10049 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10050}
10051
Avi Kivity51aa01d2010-07-20 14:31:20 +030010052static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010053{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010054 u32 exit_intr_info = 0;
10055 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010056
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010057 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10058 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010059 return;
10060
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010061 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10062 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10063 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010064
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010065 /* if exit due to PF check for async PF */
10066 if (is_page_fault(exit_intr_info))
10067 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10068
Andi Kleena0861c02009-06-08 17:37:09 +080010069 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010070 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10071 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010072 kvm_machine_check();
10073
Gleb Natapov20f65982009-05-11 13:35:55 +030010074 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010075 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010076 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010077 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010078 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010079 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010080}
Gleb Natapov20f65982009-05-11 13:35:55 +030010081
Yang Zhanga547c6d2013-04-11 19:25:10 +080010082static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10083{
10084 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10085
Yang Zhanga547c6d2013-04-11 19:25:10 +080010086 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10087 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10088 unsigned int vector;
10089 unsigned long entry;
10090 gate_desc *desc;
10091 struct vcpu_vmx *vmx = to_vmx(vcpu);
10092#ifdef CONFIG_X86_64
10093 unsigned long tmp;
10094#endif
10095
10096 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10097 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010098 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010099 asm volatile(
10100#ifdef CONFIG_X86_64
10101 "mov %%" _ASM_SP ", %[sp]\n\t"
10102 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10103 "push $%c[ss]\n\t"
10104 "push %[sp]\n\t"
10105#endif
10106 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010107 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010108 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010109 :
10110#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010111 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010112#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010113 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010114 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010115 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010116 [ss]"i"(__KERNEL_DS),
10117 [cs]"i"(__KERNEL_CS)
10118 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010119 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010120}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010121STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010122
Tom Lendackybc226f02018-05-10 22:06:39 +020010123static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010124{
Tom Lendackybc226f02018-05-10 22:06:39 +020010125 switch (index) {
10126 case MSR_IA32_SMBASE:
10127 /*
10128 * We cannot do SMM unless we can run the guest in big
10129 * real mode.
10130 */
10131 return enable_unrestricted_guest || emulate_invalid_guest_state;
10132 case MSR_AMD64_VIRT_SPEC_CTRL:
10133 /* This is AMD only. */
10134 return false;
10135 default:
10136 return true;
10137 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010138}
10139
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010140static bool vmx_mpx_supported(void)
10141{
10142 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10143 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10144}
10145
Wanpeng Li55412b22014-12-02 19:21:30 +080010146static bool vmx_xsaves_supported(void)
10147{
10148 return vmcs_config.cpu_based_2nd_exec_ctrl &
10149 SECONDARY_EXEC_XSAVES;
10150}
10151
Avi Kivity51aa01d2010-07-20 14:31:20 +030010152static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10153{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010154 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010155 bool unblock_nmi;
10156 u8 vector;
10157 bool idtv_info_valid;
10158
10159 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010160
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010161 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010162 if (vmx->loaded_vmcs->nmi_known_unmasked)
10163 return;
10164 /*
10165 * Can't use vmx->exit_intr_info since we're not sure what
10166 * the exit reason is.
10167 */
10168 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10169 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10170 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10171 /*
10172 * SDM 3: 27.7.1.2 (September 2008)
10173 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10174 * a guest IRET fault.
10175 * SDM 3: 23.2.2 (September 2008)
10176 * Bit 12 is undefined in any of the following cases:
10177 * If the VM exit sets the valid bit in the IDT-vectoring
10178 * information field.
10179 * If the VM exit is due to a double fault.
10180 */
10181 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10182 vector != DF_VECTOR && !idtv_info_valid)
10183 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10184 GUEST_INTR_STATE_NMI);
10185 else
10186 vmx->loaded_vmcs->nmi_known_unmasked =
10187 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10188 & GUEST_INTR_STATE_NMI);
10189 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10190 vmx->loaded_vmcs->vnmi_blocked_time +=
10191 ktime_to_ns(ktime_sub(ktime_get(),
10192 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010193}
10194
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010195static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010196 u32 idt_vectoring_info,
10197 int instr_len_field,
10198 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010199{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010200 u8 vector;
10201 int type;
10202 bool idtv_info_valid;
10203
10204 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010205
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010206 vcpu->arch.nmi_injected = false;
10207 kvm_clear_exception_queue(vcpu);
10208 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010209
10210 if (!idtv_info_valid)
10211 return;
10212
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010213 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010214
Avi Kivity668f6122008-07-02 09:28:55 +030010215 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10216 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010217
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010218 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010219 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010220 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010221 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010222 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010223 * Clear bit "block by NMI" before VM entry if a NMI
10224 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010225 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010226 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010227 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010228 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010229 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010230 /* fall through */
10231 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010232 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010233 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010234 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010235 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010236 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010237 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010238 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010239 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010240 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010241 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010242 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010243 break;
10244 default:
10245 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010246 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010247}
10248
Avi Kivity83422e12010-07-20 14:43:23 +030010249static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10250{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010251 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010252 VM_EXIT_INSTRUCTION_LEN,
10253 IDT_VECTORING_ERROR_CODE);
10254}
10255
Avi Kivityb463a6f2010-07-20 15:06:17 +030010256static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10257{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010258 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010259 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10260 VM_ENTRY_INSTRUCTION_LEN,
10261 VM_ENTRY_EXCEPTION_ERROR_CODE);
10262
10263 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10264}
10265
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010266static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10267{
10268 int i, nr_msrs;
10269 struct perf_guest_switch_msr *msrs;
10270
10271 msrs = perf_guest_get_msrs(&nr_msrs);
10272
10273 if (!msrs)
10274 return;
10275
10276 for (i = 0; i < nr_msrs; i++)
10277 if (msrs[i].host == msrs[i].guest)
10278 clear_atomic_switch_msr(vmx, msrs[i].msr);
10279 else
10280 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10281 msrs[i].host);
10282}
10283
Jiang Biao33365e72016-11-03 15:03:37 +080010284static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010285{
10286 struct vcpu_vmx *vmx = to_vmx(vcpu);
10287 u64 tscl;
10288 u32 delta_tsc;
10289
10290 if (vmx->hv_deadline_tsc == -1)
10291 return;
10292
10293 tscl = rdtsc();
10294 if (vmx->hv_deadline_tsc > tscl)
10295 /* sure to be 32 bit only because checked on set_hv_timer */
10296 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10297 cpu_preemption_timer_multi);
10298 else
10299 delta_tsc = 0;
10300
10301 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10302}
10303
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010304static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010305{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010306 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010307 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010308
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010309 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010310 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010311 vmx->loaded_vmcs->soft_vnmi_blocked))
10312 vmx->loaded_vmcs->entry_time = ktime_get();
10313
Avi Kivity104f2262010-11-18 13:12:52 +020010314 /* Don't enter VMX if guest state is invalid, let the exit handler
10315 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010316 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010317 return;
10318
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010319 if (vmx->ple_window_dirty) {
10320 vmx->ple_window_dirty = false;
10321 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10322 }
10323
Abel Gordon012f83c2013-04-18 14:39:25 +030010324 if (vmx->nested.sync_shadow_vmcs) {
10325 copy_vmcs12_to_shadow(vmx);
10326 vmx->nested.sync_shadow_vmcs = false;
10327 }
10328
Avi Kivity104f2262010-11-18 13:12:52 +020010329 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10330 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10331 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10332 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10333
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010334 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010335 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010336 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010337 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010338 }
10339
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010340 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010341 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010342 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010343 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010344 }
10345
Avi Kivity104f2262010-11-18 13:12:52 +020010346 /* When single-stepping over STI and MOV SS, we must clear the
10347 * corresponding interruptibility bits in the guest state. Otherwise
10348 * vmentry fails as it then expects bit 14 (BS) in pending debug
10349 * exceptions being set, but that's not correct for the guest debugging
10350 * case. */
10351 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10352 vmx_set_interrupt_shadow(vcpu, 0);
10353
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010354 if (static_cpu_has(X86_FEATURE_PKU) &&
10355 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10356 vcpu->arch.pkru != vmx->host_pkru)
10357 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010358
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010359 atomic_switch_perf_msrs(vmx);
10360
Yunhong Jiang64672c92016-06-13 14:19:59 -070010361 vmx_arm_hv_timer(vcpu);
10362
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010363 /*
10364 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10365 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10366 * is no need to worry about the conditional branch over the wrmsr
10367 * being speculatively taken.
10368 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010369 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010370
Nadav Har'Eld462b812011-05-24 15:26:10 +030010371 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010372
10373 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10374 (unsigned long)&current_evmcs->host_rsp : 0;
10375
Avi Kivity104f2262010-11-18 13:12:52 +020010376 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010377 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010378 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10379 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10380 "push %%" _ASM_CX " \n\t"
10381 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010382 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010383 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010384 /* Avoid VMWRITE when Enlightened VMCS is in use */
10385 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10386 "jz 2f \n\t"
10387 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10388 "jmp 1f \n\t"
10389 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010390 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010391 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010392 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010393 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10394 "mov %%cr2, %%" _ASM_DX " \n\t"
10395 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010396 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010397 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010398 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010399 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010400 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010401 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010402 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10403 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10404 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10405 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10406 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10407 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010408#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010409 "mov %c[r8](%0), %%r8 \n\t"
10410 "mov %c[r9](%0), %%r9 \n\t"
10411 "mov %c[r10](%0), %%r10 \n\t"
10412 "mov %c[r11](%0), %%r11 \n\t"
10413 "mov %c[r12](%0), %%r12 \n\t"
10414 "mov %c[r13](%0), %%r13 \n\t"
10415 "mov %c[r14](%0), %%r14 \n\t"
10416 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010417#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010418 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010419
Avi Kivity6aa8b732006-12-10 02:21:36 -080010420 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010421 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010422 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010423 "jmp 2f \n\t"
10424 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10425 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010426 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010427 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010428 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010429 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010430 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10431 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10432 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10433 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10434 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10435 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10436 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010437#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010438 "mov %%r8, %c[r8](%0) \n\t"
10439 "mov %%r9, %c[r9](%0) \n\t"
10440 "mov %%r10, %c[r10](%0) \n\t"
10441 "mov %%r11, %c[r11](%0) \n\t"
10442 "mov %%r12, %c[r12](%0) \n\t"
10443 "mov %%r13, %c[r13](%0) \n\t"
10444 "mov %%r14, %c[r14](%0) \n\t"
10445 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010446 "xor %%r8d, %%r8d \n\t"
10447 "xor %%r9d, %%r9d \n\t"
10448 "xor %%r10d, %%r10d \n\t"
10449 "xor %%r11d, %%r11d \n\t"
10450 "xor %%r12d, %%r12d \n\t"
10451 "xor %%r13d, %%r13d \n\t"
10452 "xor %%r14d, %%r14d \n\t"
10453 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010454#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010455 "mov %%cr2, %%" _ASM_AX " \n\t"
10456 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010457
Jim Mattson0cb5b302018-01-03 14:31:38 -080010458 "xor %%eax, %%eax \n\t"
10459 "xor %%ebx, %%ebx \n\t"
10460 "xor %%esi, %%esi \n\t"
10461 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010462 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010463 ".pushsection .rodata \n\t"
10464 ".global vmx_return \n\t"
10465 "vmx_return: " _ASM_PTR " 2b \n\t"
10466 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010467 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010468 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010469 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010470 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010471 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10472 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10473 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10474 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10475 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10476 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10477 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010478#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010479 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10480 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10481 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10482 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10483 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10484 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10485 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10486 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010487#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010488 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10489 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010490 : "cc", "memory"
10491#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010492 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010493 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010494#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010495 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010496#endif
10497 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010498
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010499 /*
10500 * We do not use IBRS in the kernel. If this vCPU has used the
10501 * SPEC_CTRL MSR it may have left it on; save the value and
10502 * turn it off. This is much more efficient than blindly adding
10503 * it to the atomic save/restore list. Especially as the former
10504 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10505 *
10506 * For non-nested case:
10507 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10508 * save it.
10509 *
10510 * For nested case:
10511 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10512 * save it.
10513 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010514 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010515 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010516
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010517 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010518
David Woodhouse117cc7a2018-01-12 11:11:27 +000010519 /* Eliminate branch target predictions from guest mode */
10520 vmexit_fill_RSB();
10521
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010522 /* All fields are clean at this point */
10523 if (static_branch_unlikely(&enable_evmcs))
10524 current_evmcs->hv_clean_fields |=
10525 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10526
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010527 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010528 if (vmx->host_debugctlmsr)
10529 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010530
Avi Kivityaa67f602012-08-01 16:48:03 +030010531#ifndef CONFIG_X86_64
10532 /*
10533 * The sysexit path does not restore ds/es, so we must set them to
10534 * a reasonable value ourselves.
10535 *
10536 * We can't defer this to vmx_load_host_state() since that function
10537 * may be executed in interrupt context, which saves and restore segments
10538 * around it, nullifying its effect.
10539 */
10540 loadsegment(ds, __USER_DS);
10541 loadsegment(es, __USER_DS);
10542#endif
10543
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010544 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010545 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010546 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010547 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010548 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010549 vcpu->arch.regs_dirty = 0;
10550
Gleb Natapove0b890d2013-09-25 12:51:33 +030010551 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010552 * eager fpu is enabled if PKEY is supported and CR4 is switched
10553 * back on host, so it is safe to read guest PKRU from current
10554 * XSAVE.
10555 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010556 if (static_cpu_has(X86_FEATURE_PKU) &&
10557 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10558 vcpu->arch.pkru = __read_pkru();
10559 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010560 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010561 }
10562
Gleb Natapove0b890d2013-09-25 12:51:33 +030010563 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010564 vmx->idt_vectoring_info = 0;
10565
10566 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10567 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10568 return;
10569
10570 vmx->loaded_vmcs->launched = 1;
10571 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010572
Avi Kivity51aa01d2010-07-20 14:31:20 +030010573 vmx_complete_atomic_exit(vmx);
10574 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010575 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010576}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010577STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010578
Sean Christopherson434a1e92018-03-20 12:17:18 -070010579static struct kvm *vmx_vm_alloc(void)
10580{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010581 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010582 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010583}
10584
10585static void vmx_vm_free(struct kvm *kvm)
10586{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010587 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010588}
10589
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010590static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010591{
10592 struct vcpu_vmx *vmx = to_vmx(vcpu);
10593 int cpu;
10594
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010595 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010596 return;
10597
10598 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010599 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010600 vmx_vcpu_put(vcpu);
10601 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010602 put_cpu();
10603}
10604
Jim Mattson2f1fe812016-07-08 15:36:06 -070010605/*
10606 * Ensure that the current vmcs of the logical processor is the
10607 * vmcs01 of the vcpu before calling free_nested().
10608 */
10609static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10610{
10611 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010612
Christoffer Dallec7660c2017-12-04 21:35:23 +010010613 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010614 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010615 free_nested(vmx);
10616 vcpu_put(vcpu);
10617}
10618
Avi Kivity6aa8b732006-12-10 02:21:36 -080010619static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10620{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010621 struct vcpu_vmx *vmx = to_vmx(vcpu);
10622
Kai Huang843e4332015-01-28 10:54:28 +080010623 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010624 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010625 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010626 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010627 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010628 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010629 kfree(vmx->guest_msrs);
10630 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010631 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010632}
10633
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010634static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010635{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010636 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010637 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010638 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010639 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010640
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010641 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010642 return ERR_PTR(-ENOMEM);
10643
Wanpeng Li991e7a02015-09-16 17:30:05 +080010644 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010645
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010646 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10647 if (err)
10648 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010649
Peter Feiner4e595162016-07-07 14:49:58 -070010650 err = -ENOMEM;
10651
10652 /*
10653 * If PML is turned on, failure on enabling PML just results in failure
10654 * of creating the vcpu, therefore we can simplify PML logic (by
10655 * avoiding dealing with cases, such as enabling PML partially on vcpus
10656 * for the guest, etc.
10657 */
10658 if (enable_pml) {
10659 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10660 if (!vmx->pml_pg)
10661 goto uninit_vcpu;
10662 }
10663
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010664 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010665 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10666 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010667
Peter Feiner4e595162016-07-07 14:49:58 -070010668 if (!vmx->guest_msrs)
10669 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010670
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010671 err = alloc_loaded_vmcs(&vmx->vmcs01);
10672 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010673 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010674
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010675 msr_bitmap = vmx->vmcs01.msr_bitmap;
10676 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10677 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10678 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10679 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10680 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10681 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10682 vmx->msr_bitmap_mode = 0;
10683
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010684 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010685 cpu = get_cpu();
10686 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010687 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010688 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010689 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010690 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010691 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010692 err = alloc_apic_access_page(kvm);
10693 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010694 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010695 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010696
Sean Christophersone90008d2018-03-05 12:04:37 -080010697 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010698 err = init_rmode_identity_map(kvm);
10699 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010700 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010701 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010702
Wanpeng Li5c614b32015-10-13 09:18:36 -070010703 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010704 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10705 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010706 vmx->nested.vpid02 = allocate_vpid();
10707 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010708
Wincy Van705699a2015-02-03 23:58:17 +080010709 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010710 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010711
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010712 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10713
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010714 /*
10715 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10716 * or POSTED_INTR_WAKEUP_VECTOR.
10717 */
10718 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10719 vmx->pi_desc.sn = 1;
10720
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010721 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010722
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010723free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010724 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010725 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010726free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010727 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010728free_pml:
10729 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010730uninit_vcpu:
10731 kvm_vcpu_uninit(&vmx->vcpu);
10732free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010733 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010734 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010735 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010736}
10737
Wanpeng Lib31c1142018-03-12 04:53:04 -070010738static int vmx_vm_init(struct kvm *kvm)
10739{
Tianyu Lan877ad952018-07-19 08:40:23 +000010740 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
10741
Wanpeng Lib31c1142018-03-12 04:53:04 -070010742 if (!ple_gap)
10743 kvm->arch.pause_in_guest = true;
10744 return 0;
10745}
10746
Yang, Sheng002c7f72007-07-31 14:23:01 +030010747static void __init vmx_check_processor_compat(void *rtn)
10748{
10749 struct vmcs_config vmcs_conf;
10750
10751 *(int *)rtn = 0;
10752 if (setup_vmcs_config(&vmcs_conf) < 0)
10753 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010754 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010755 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10756 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10757 smp_processor_id());
10758 *(int *)rtn = -EIO;
10759 }
10760}
10761
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010762static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010763{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010764 u8 cache;
10765 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010766
Sheng Yang522c68c2009-04-27 20:35:43 +080010767 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010768 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010769 * 2. EPT with VT-d:
10770 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010771 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010772 * b. VT-d with snooping control feature: snooping control feature of
10773 * VT-d engine can guarantee the cache correctness. Just set it
10774 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010775 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010776 * consistent with host MTRR
10777 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010778 if (is_mmio) {
10779 cache = MTRR_TYPE_UNCACHABLE;
10780 goto exit;
10781 }
10782
10783 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010784 ipat = VMX_EPT_IPAT_BIT;
10785 cache = MTRR_TYPE_WRBACK;
10786 goto exit;
10787 }
10788
10789 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10790 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010791 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010792 cache = MTRR_TYPE_WRBACK;
10793 else
10794 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010795 goto exit;
10796 }
10797
Xiao Guangrongff536042015-06-15 16:55:22 +080010798 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010799
10800exit:
10801 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010802}
10803
Sheng Yang17cc3932010-01-05 19:02:27 +080010804static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010805{
Sheng Yang878403b2010-01-05 19:02:29 +080010806 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10807 return PT_DIRECTORY_LEVEL;
10808 else
10809 /* For shadow and EPT supported 1GB page */
10810 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010811}
10812
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010813static void vmcs_set_secondary_exec_control(u32 new_ctl)
10814{
10815 /*
10816 * These bits in the secondary execution controls field
10817 * are dynamic, the others are mostly based on the hypervisor
10818 * architecture and the guest's CPUID. Do not touch the
10819 * dynamic bits.
10820 */
10821 u32 mask =
10822 SECONDARY_EXEC_SHADOW_VMCS |
10823 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010824 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10825 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010826
10827 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10828
10829 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10830 (new_ctl & ~mask) | (cur_ctl & mask));
10831}
10832
David Matlack8322ebb2016-11-29 18:14:09 -080010833/*
10834 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10835 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10836 */
10837static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10838{
10839 struct vcpu_vmx *vmx = to_vmx(vcpu);
10840 struct kvm_cpuid_entry2 *entry;
10841
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010842 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10843 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010844
10845#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10846 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010847 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010848} while (0)
10849
10850 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10851 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10852 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10853 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10854 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10855 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10856 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10857 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10858 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10859 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10860 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10861 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10862 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10863 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10864 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10865
10866 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10867 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10868 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10869 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10870 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010871 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010872
10873#undef cr4_fixed1_update
10874}
10875
Sheng Yang0e851882009-12-18 16:48:46 +080010876static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10877{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010878 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010879
Paolo Bonzini80154d72017-08-24 13:55:35 +020010880 if (cpu_has_secondary_exec_ctrls()) {
10881 vmx_compute_secondary_exec_control(vmx);
10882 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010883 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010884
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010885 if (nested_vmx_allowed(vcpu))
10886 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10887 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10888 else
10889 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10890 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010891
10892 if (nested_vmx_allowed(vcpu))
10893 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010894}
10895
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010896static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10897{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010898 if (func == 1 && nested)
10899 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010900}
10901
Yang Zhang25d92082013-08-06 12:00:32 +030010902static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10903 struct x86_exception *fault)
10904{
Jan Kiszka533558b2014-01-04 18:47:20 +010010905 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010906 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010907 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010908 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010909
Bandan Dasc5f983f2017-05-05 15:25:14 -040010910 if (vmx->nested.pml_full) {
10911 exit_reason = EXIT_REASON_PML_FULL;
10912 vmx->nested.pml_full = false;
10913 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10914 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010915 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010916 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010917 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010918
10919 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010920 vmcs12->guest_physical_address = fault->address;
10921}
10922
Peter Feiner995f00a2017-06-30 17:26:32 -070010923static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10924{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010925 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010926}
10927
Nadav Har'El155a97a2013-08-05 11:07:16 +030010928/* Callbacks for nested_ept_init_mmu_context: */
10929
10930static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10931{
10932 /* return the page table to be shadowed - in our case, EPT12 */
10933 return get_vmcs12(vcpu)->ept_pointer;
10934}
10935
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010936static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010937{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010938 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010939 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010940 return 1;
10941
Paolo Bonziniad896af2013-10-02 16:56:14 +020010942 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010943 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010944 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070010945 nested_ept_ad_enabled(vcpu),
10946 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010947 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10948 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10949 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10950
10951 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010952 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010953}
10954
10955static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10956{
10957 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10958}
10959
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010960static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10961 u16 error_code)
10962{
10963 bool inequality, bit;
10964
10965 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10966 inequality =
10967 (error_code & vmcs12->page_fault_error_code_mask) !=
10968 vmcs12->page_fault_error_code_match;
10969 return inequality ^ bit;
10970}
10971
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010972static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10973 struct x86_exception *fault)
10974{
10975 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10976
10977 WARN_ON(!is_guest_mode(vcpu));
10978
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010979 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10980 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010981 vmcs12->vm_exit_intr_error_code = fault->error_code;
10982 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10983 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10984 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10985 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010986 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010987 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010988 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010989}
10990
Paolo Bonzinic9923842017-12-13 14:16:30 +010010991static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10992 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010993
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010994static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010995{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010996 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010997 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010998 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010999 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011000
11001 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011002 /*
11003 * Translate L1 physical address to host physical
11004 * address for vmcs02. Keep the page pinned, so this
11005 * physical address remains valid. We keep a reference
11006 * to it so we can release it later.
11007 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011008 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011009 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011010 vmx->nested.apic_access_page = NULL;
11011 }
11012 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011013 /*
11014 * If translation failed, no matter: This feature asks
11015 * to exit when accessing the given address, and if it
11016 * can never be accessed, this feature won't do
11017 * anything anyway.
11018 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011019 if (!is_error_page(page)) {
11020 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011021 hpa = page_to_phys(vmx->nested.apic_access_page);
11022 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11023 } else {
11024 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11025 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11026 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011027 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011028
11029 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011030 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011031 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011032 vmx->nested.virtual_apic_page = NULL;
11033 }
11034 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011035
11036 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011037 * If translation failed, VM entry will fail because
11038 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11039 * Failing the vm entry is _not_ what the processor
11040 * does but it's basically the only possibility we
11041 * have. We could still enter the guest if CR8 load
11042 * exits are enabled, CR8 store exits are enabled, and
11043 * virtualize APIC access is disabled; in this case
11044 * the processor would never use the TPR shadow and we
11045 * could simply clear the bit from the execution
11046 * control. But such a configuration is useless, so
11047 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011048 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011049 if (!is_error_page(page)) {
11050 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011051 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11052 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11053 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011054 }
11055
Wincy Van705699a2015-02-03 23:58:17 +080011056 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011057 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11058 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011059 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011060 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011061 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011062 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11063 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011064 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011065 vmx->nested.pi_desc_page = page;
11066 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011067 vmx->nested.pi_desc =
11068 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11069 (unsigned long)(vmcs12->posted_intr_desc_addr &
11070 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011071 vmcs_write64(POSTED_INTR_DESC_ADDR,
11072 page_to_phys(vmx->nested.pi_desc_page) +
11073 (unsigned long)(vmcs12->posted_intr_desc_addr &
11074 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011075 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011076 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011077 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11078 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011079 else
11080 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11081 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011082}
11083
Jan Kiszkaf4124502014-03-07 20:03:13 +010011084static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11085{
11086 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11087 struct vcpu_vmx *vmx = to_vmx(vcpu);
11088
11089 if (vcpu->arch.virtual_tsc_khz == 0)
11090 return;
11091
11092 /* Make sure short timeouts reliably trigger an immediate vmexit.
11093 * hrtimer_start does not guarantee this. */
11094 if (preemption_timeout <= 1) {
11095 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11096 return;
11097 }
11098
11099 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11100 preemption_timeout *= 1000000;
11101 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11102 hrtimer_start(&vmx->nested.preemption_timer,
11103 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11104}
11105
Jim Mattson56a20512017-07-06 16:33:06 -070011106static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11107 struct vmcs12 *vmcs12)
11108{
11109 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11110 return 0;
11111
11112 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11113 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11114 return -EINVAL;
11115
11116 return 0;
11117}
11118
Wincy Van3af18d92015-02-03 23:49:31 +080011119static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11120 struct vmcs12 *vmcs12)
11121{
Wincy Van3af18d92015-02-03 23:49:31 +080011122 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11123 return 0;
11124
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011125 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011126 return -EINVAL;
11127
11128 return 0;
11129}
11130
Jim Mattson712b12d2017-08-24 13:24:47 -070011131static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11132 struct vmcs12 *vmcs12)
11133{
11134 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11135 return 0;
11136
11137 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11138 return -EINVAL;
11139
11140 return 0;
11141}
11142
Wincy Van3af18d92015-02-03 23:49:31 +080011143/*
11144 * Merge L0's and L1's MSR bitmap, return false to indicate that
11145 * we do not use the hardware.
11146 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011147static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11148 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011149{
Wincy Van82f0dd42015-02-03 23:57:18 +080011150 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011151 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011152 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011153 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011154 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011155 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011156 *
11157 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11158 * ensures that we do not accidentally generate an L02 MSR bitmap
11159 * from the L12 MSR bitmap that is too permissive.
11160 * 2. That L1 or L2s have actually used the MSR. This avoids
11161 * unnecessarily merging of the bitmap if the MSR is unused. This
11162 * works properly because we only update the L01 MSR bitmap lazily.
11163 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11164 * updated to reflect this when L1 (or its L2s) actually write to
11165 * the MSR.
11166 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011167 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11168 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011169
Paolo Bonzinic9923842017-12-13 14:16:30 +010011170 /* Nothing to do if the MSR bitmap is not in use. */
11171 if (!cpu_has_vmx_msr_bitmap() ||
11172 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11173 return false;
11174
Ashok Raj15d45072018-02-01 22:59:43 +010011175 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011176 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011177 return false;
11178
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011179 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11180 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011181 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011182
Radim Krčmářd048c092016-08-08 20:16:22 +020011183 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011184 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11185 /*
11186 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11187 * just lets the processor take the value from the virtual-APIC page;
11188 * take those 256 bits directly from the L1 bitmap.
11189 */
11190 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11191 unsigned word = msr / BITS_PER_LONG;
11192 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11193 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011194 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011195 } else {
11196 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11197 unsigned word = msr / BITS_PER_LONG;
11198 msr_bitmap_l0[word] = ~0;
11199 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11200 }
11201 }
11202
11203 nested_vmx_disable_intercept_for_msr(
11204 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011205 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011206 MSR_TYPE_W);
11207
11208 if (nested_cpu_has_vid(vmcs12)) {
11209 nested_vmx_disable_intercept_for_msr(
11210 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011211 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011212 MSR_TYPE_W);
11213 nested_vmx_disable_intercept_for_msr(
11214 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011215 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011216 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011217 }
Ashok Raj15d45072018-02-01 22:59:43 +010011218
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011219 if (spec_ctrl)
11220 nested_vmx_disable_intercept_for_msr(
11221 msr_bitmap_l1, msr_bitmap_l0,
11222 MSR_IA32_SPEC_CTRL,
11223 MSR_TYPE_R | MSR_TYPE_W);
11224
Ashok Raj15d45072018-02-01 22:59:43 +010011225 if (pred_cmd)
11226 nested_vmx_disable_intercept_for_msr(
11227 msr_bitmap_l1, msr_bitmap_l0,
11228 MSR_IA32_PRED_CMD,
11229 MSR_TYPE_W);
11230
Wincy Vanf2b93282015-02-03 23:56:03 +080011231 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011232 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011233
11234 return true;
11235}
11236
Liran Alon61ada742018-06-23 02:35:08 +030011237static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11238 struct vmcs12 *vmcs12)
11239{
11240 struct vmcs12 *shadow;
11241 struct page *page;
11242
11243 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11244 vmcs12->vmcs_link_pointer == -1ull)
11245 return;
11246
11247 shadow = get_shadow_vmcs12(vcpu);
11248 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11249
11250 memcpy(shadow, kmap(page), VMCS12_SIZE);
11251
11252 kunmap(page);
11253 kvm_release_page_clean(page);
11254}
11255
11256static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11257 struct vmcs12 *vmcs12)
11258{
11259 struct vcpu_vmx *vmx = to_vmx(vcpu);
11260
11261 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11262 vmcs12->vmcs_link_pointer == -1ull)
11263 return;
11264
11265 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11266 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11267}
11268
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011269static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11270 struct vmcs12 *vmcs12)
11271{
11272 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11273 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11274 return -EINVAL;
11275 else
11276 return 0;
11277}
11278
Wincy Vanf2b93282015-02-03 23:56:03 +080011279static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11280 struct vmcs12 *vmcs12)
11281{
Wincy Van82f0dd42015-02-03 23:57:18 +080011282 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011283 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011284 !nested_cpu_has_vid(vmcs12) &&
11285 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011286 return 0;
11287
11288 /*
11289 * If virtualize x2apic mode is enabled,
11290 * virtualize apic access must be disabled.
11291 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011292 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11293 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011294 return -EINVAL;
11295
Wincy Van608406e2015-02-03 23:57:51 +080011296 /*
11297 * If virtual interrupt delivery is enabled,
11298 * we must exit on external interrupts.
11299 */
11300 if (nested_cpu_has_vid(vmcs12) &&
11301 !nested_exit_on_intr(vcpu))
11302 return -EINVAL;
11303
Wincy Van705699a2015-02-03 23:58:17 +080011304 /*
11305 * bits 15:8 should be zero in posted_intr_nv,
11306 * the descriptor address has been already checked
11307 * in nested_get_vmcs12_pages.
11308 */
11309 if (nested_cpu_has_posted_intr(vmcs12) &&
11310 (!nested_cpu_has_vid(vmcs12) ||
11311 !nested_exit_intr_ack_set(vcpu) ||
11312 vmcs12->posted_intr_nv & 0xff00))
11313 return -EINVAL;
11314
Wincy Vanf2b93282015-02-03 23:56:03 +080011315 /* tpr shadow is needed by all apicv features. */
11316 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11317 return -EINVAL;
11318
11319 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011320}
11321
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011322static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11323 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011324 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011325{
Liran Alone2536742018-06-23 02:35:02 +030011326 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011327 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011328 u64 count, addr;
11329
Liran Alone2536742018-06-23 02:35:02 +030011330 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11331 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011332 WARN_ON(1);
11333 return -EINVAL;
11334 }
11335 if (count == 0)
11336 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011337 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011338 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11339 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011340 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011341 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11342 addr_field, maxphyaddr, count, addr);
11343 return -EINVAL;
11344 }
11345 return 0;
11346}
11347
11348static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11349 struct vmcs12 *vmcs12)
11350{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011351 if (vmcs12->vm_exit_msr_load_count == 0 &&
11352 vmcs12->vm_exit_msr_store_count == 0 &&
11353 vmcs12->vm_entry_msr_load_count == 0)
11354 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011355 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011356 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011357 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011358 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011359 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011360 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011361 return -EINVAL;
11362 return 0;
11363}
11364
Bandan Dasc5f983f2017-05-05 15:25:14 -040011365static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11366 struct vmcs12 *vmcs12)
11367{
11368 u64 address = vmcs12->pml_address;
11369 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11370
11371 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11372 if (!nested_cpu_has_ept(vmcs12) ||
11373 !IS_ALIGNED(address, 4096) ||
11374 address >> maxphyaddr)
11375 return -EINVAL;
11376 }
11377
11378 return 0;
11379}
11380
Liran Alona8a7c022018-06-23 02:35:06 +030011381static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11382 struct vmcs12 *vmcs12)
11383{
11384 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11385 return 0;
11386
11387 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11388 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11389 return -EINVAL;
11390
11391 return 0;
11392}
11393
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011394static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11395 struct vmx_msr_entry *e)
11396{
11397 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011398 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011399 return -EINVAL;
11400 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11401 e->index == MSR_IA32_UCODE_REV)
11402 return -EINVAL;
11403 if (e->reserved != 0)
11404 return -EINVAL;
11405 return 0;
11406}
11407
11408static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11409 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011410{
11411 if (e->index == MSR_FS_BASE ||
11412 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011413 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11414 nested_vmx_msr_check_common(vcpu, e))
11415 return -EINVAL;
11416 return 0;
11417}
11418
11419static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11420 struct vmx_msr_entry *e)
11421{
11422 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11423 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011424 return -EINVAL;
11425 return 0;
11426}
11427
11428/*
11429 * Load guest's/host's msr at nested entry/exit.
11430 * return 0 for success, entry index for failure.
11431 */
11432static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11433{
11434 u32 i;
11435 struct vmx_msr_entry e;
11436 struct msr_data msr;
11437
11438 msr.host_initiated = false;
11439 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011440 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11441 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011442 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011443 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11444 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011445 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011446 }
11447 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011448 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011449 "%s check failed (%u, 0x%x, 0x%x)\n",
11450 __func__, i, e.index, e.reserved);
11451 goto fail;
11452 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011453 msr.index = e.index;
11454 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011455 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011456 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011457 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11458 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011459 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011460 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011461 }
11462 return 0;
11463fail:
11464 return i + 1;
11465}
11466
11467static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11468{
11469 u32 i;
11470 struct vmx_msr_entry e;
11471
11472 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011473 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011474 if (kvm_vcpu_read_guest(vcpu,
11475 gpa + i * sizeof(e),
11476 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011477 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011478 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11479 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011480 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011481 }
11482 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011483 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011484 "%s check failed (%u, 0x%x, 0x%x)\n",
11485 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011486 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011487 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011488 msr_info.host_initiated = false;
11489 msr_info.index = e.index;
11490 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011491 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011492 "%s cannot read MSR (%u, 0x%x)\n",
11493 __func__, i, e.index);
11494 return -EINVAL;
11495 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011496 if (kvm_vcpu_write_guest(vcpu,
11497 gpa + i * sizeof(e) +
11498 offsetof(struct vmx_msr_entry, value),
11499 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011500 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011501 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011502 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011503 return -EINVAL;
11504 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011505 }
11506 return 0;
11507}
11508
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011509static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11510{
11511 unsigned long invalid_mask;
11512
11513 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11514 return (val & invalid_mask) == 0;
11515}
11516
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011517/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011518 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11519 * emulating VM entry into a guest with EPT enabled.
11520 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11521 * is assigned to entry_failure_code on failure.
11522 */
11523static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011524 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011525{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011526 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011527 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011528 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11529 return 1;
11530 }
11531
11532 /*
11533 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11534 * must not be dereferenced.
11535 */
11536 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11537 !nested_ept) {
11538 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11539 *entry_failure_code = ENTRY_FAIL_PDPTE;
11540 return 1;
11541 }
11542 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011543 }
11544
Junaid Shahid50c28f22018-06-27 14:59:11 -070011545 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011546 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011547
11548 vcpu->arch.cr3 = cr3;
11549 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11550
11551 kvm_init_mmu(vcpu, false);
11552
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011553 return 0;
11554}
11555
Jim Mattson6514dc32018-04-26 16:09:12 -070011556static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011557{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011558 struct vcpu_vmx *vmx = to_vmx(vcpu);
11559
11560 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11561 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11562 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11563 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11564 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11565 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11566 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11567 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11568 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11569 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11570 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11571 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11572 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11573 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11574 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11575 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11576 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11577 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11578 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11579 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11580 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11581 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11582 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11583 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11584 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11585 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11586 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11587 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11588 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11589 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11590 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011591
11592 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11593 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11594 vmcs12->guest_pending_dbg_exceptions);
11595 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11596 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11597
11598 if (nested_cpu_has_xsaves(vmcs12))
11599 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11600 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11601
11602 if (cpu_has_vmx_posted_intr())
11603 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11604
11605 /*
11606 * Whether page-faults are trapped is determined by a combination of
11607 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11608 * If enable_ept, L0 doesn't care about page faults and we should
11609 * set all of these to L1's desires. However, if !enable_ept, L0 does
11610 * care about (at least some) page faults, and because it is not easy
11611 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11612 * to exit on each and every L2 page fault. This is done by setting
11613 * MASK=MATCH=0 and (see below) EB.PF=1.
11614 * Note that below we don't need special code to set EB.PF beyond the
11615 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11616 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11617 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11618 */
11619 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11620 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11621 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11622 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11623
11624 /* All VMFUNCs are currently emulated through L0 vmexits. */
11625 if (cpu_has_vmx_vmfunc())
11626 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11627
11628 if (cpu_has_vmx_apicv()) {
11629 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11630 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11631 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11632 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11633 }
11634
11635 /*
11636 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11637 * Some constant fields are set here by vmx_set_constant_host_state().
11638 * Other fields are different per CPU, and will be set later when
11639 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11640 */
11641 vmx_set_constant_host_state(vmx);
11642
11643 /*
11644 * Set the MSR load/store lists to match L0's settings.
11645 */
11646 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11647 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11648 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11649 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11650 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11651
11652 set_cr4_guest_host_mask(vmx);
11653
11654 if (vmx_mpx_supported())
11655 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11656
11657 if (enable_vpid) {
11658 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11659 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11660 else
11661 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11662 }
11663
11664 /*
11665 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11666 */
11667 if (enable_ept) {
11668 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11669 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11670 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11671 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11672 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011673
11674 if (cpu_has_vmx_msr_bitmap())
11675 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011676}
11677
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011678/*
11679 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11680 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011681 * 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 +030011682 * guest in a way that will both be appropriate to L1's requests, and our
11683 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11684 * function also has additional necessary side-effects, like setting various
11685 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011686 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11687 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011688 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011689static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011690 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011691{
11692 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011693 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011694
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011695 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011696 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011697 vmx->nested.dirty_vmcs12 = false;
11698 }
11699
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011700 /*
11701 * First, the fields that are shadowed. This must be kept in sync
11702 * with vmx_shadow_fields.h.
11703 */
11704
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011705 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011706 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011707 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011708 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11709 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011710
11711 /*
11712 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11713 * HOST_FS_BASE, HOST_GS_BASE.
11714 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011715
Jim Mattson6514dc32018-04-26 16:09:12 -070011716 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011717 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011718 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11719 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11720 } else {
11721 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11722 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11723 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011724 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011725 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11726 vmcs12->vm_entry_intr_info_field);
11727 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11728 vmcs12->vm_entry_exception_error_code);
11729 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11730 vmcs12->vm_entry_instruction_len);
11731 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11732 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011733 vmx->loaded_vmcs->nmi_known_unmasked =
11734 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011735 } else {
11736 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11737 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011738 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011739
Jan Kiszkaf4124502014-03-07 20:03:13 +010011740 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011741
Paolo Bonzini93140062016-07-06 13:23:51 +020011742 /* Preemption timer setting is only taken from vmcs01. */
11743 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11744 exec_control |= vmcs_config.pin_based_exec_ctrl;
11745 if (vmx->hv_deadline_tsc == -1)
11746 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11747
11748 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011749 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011750 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11751 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011752 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011753 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011754 }
Wincy Van705699a2015-02-03 23:58:17 +080011755
Jan Kiszkaf4124502014-03-07 20:03:13 +010011756 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011757
Jan Kiszkaf4124502014-03-07 20:03:13 +010011758 vmx->nested.preemption_timer_expired = false;
11759 if (nested_cpu_has_preemption_timer(vmcs12))
11760 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011761
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011762 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011763 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011764
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011765 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011766 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011767 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011768 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011769 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011770 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011771 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11772 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011773 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011774 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11775 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11776 ~SECONDARY_EXEC_ENABLE_PML;
11777 exec_control |= vmcs12_exec_ctrl;
11778 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011779
Liran Alon32c7acf2018-06-23 02:35:11 +030011780 /* VMCS shadowing for L2 is emulated for now */
11781 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
11782
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011783 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011784 vmcs_write16(GUEST_INTR_STATUS,
11785 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011786
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011787 /*
11788 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11789 * nested_get_vmcs12_pages will either fix it up or
11790 * remove the VM execution control.
11791 */
11792 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11793 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11794
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011795 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11796 }
11797
Jim Mattson83bafef2016-10-04 10:48:38 -070011798 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011799 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11800 * entry, but only if the current (host) sp changed from the value
11801 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11802 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11803 * here we just force the write to happen on entry.
11804 */
11805 vmx->host_rsp = 0;
11806
11807 exec_control = vmx_exec_control(vmx); /* L0's desires */
11808 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11809 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11810 exec_control &= ~CPU_BASED_TPR_SHADOW;
11811 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011812
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011813 /*
11814 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11815 * nested_get_vmcs12_pages can't fix it up, the illegal value
11816 * will result in a VM entry failure.
11817 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011818 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011819 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011820 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011821 } else {
11822#ifdef CONFIG_X86_64
11823 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11824 CPU_BASED_CR8_STORE_EXITING;
11825#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011826 }
11827
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011828 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011829 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11830 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011831 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011832 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11833 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11834
11835 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11836
11837 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11838 * bitwise-or of what L1 wants to trap for L2, and what we want to
11839 * trap. Note that CR0.TS also needs updating - we do this later.
11840 */
11841 update_exception_bitmap(vcpu);
11842 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11843 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11844
Nadav Har'El8049d652013-08-05 11:07:06 +030011845 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11846 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11847 * bits are further modified by vmx_set_efer() below.
11848 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011849 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011850
11851 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11852 * emulated by vmx_set_efer(), below.
11853 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011854 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011855 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11856 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011857 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11858
Jim Mattson6514dc32018-04-26 16:09:12 -070011859 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011860 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011861 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011862 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011863 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011864 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011865 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011866
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011867 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11868
Peter Feinerc95ba922016-08-17 09:36:47 -070011869 if (kvm_has_tsc_control)
11870 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011871
11872 if (enable_vpid) {
11873 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011874 * There is no direct mapping between vpid02 and vpid12, the
11875 * vpid02 is per-vCPU for L0 and reused while the value of
11876 * vpid12 is changed w/ one invvpid during nested vmentry.
11877 * The vpid12 is allocated by L1 for L2, so it will not
11878 * influence global bitmap(for vpid01 and vpid02 allocation)
11879 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011880 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011881 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011882 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11883 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011884 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011885 }
11886 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011887 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011888 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011889 }
11890
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011891 if (enable_pml) {
11892 /*
11893 * Conceptually we want to copy the PML address and index from
11894 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11895 * since we always flush the log on each vmexit, this happens
11896 * to be equivalent to simply resetting the fields in vmcs02.
11897 */
11898 ASSERT(vmx->pml_pg);
11899 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11900 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11901 }
11902
Nadav Har'El155a97a2013-08-05 11:07:16 +030011903 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011904 if (nested_ept_init_mmu_context(vcpu)) {
11905 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11906 return 1;
11907 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011908 } else if (nested_cpu_has2(vmcs12,
11909 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011910 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011911 }
11912
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011913 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011914 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11915 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011916 * The CR0_READ_SHADOW is what L2 should have expected to read given
11917 * the specifications by L1; It's not enough to take
11918 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11919 * have more bits than L1 expected.
11920 */
11921 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11922 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11923
11924 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11925 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11926
Jim Mattson6514dc32018-04-26 16:09:12 -070011927 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011928 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011929 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11930 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11931 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11932 else
11933 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11934 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11935 vmx_set_efer(vcpu, vcpu->arch.efer);
11936
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011937 /*
11938 * Guest state is invalid and unrestricted guest is disabled,
11939 * which means L1 attempted VMEntry to L2 with invalid state.
11940 * Fail the VMEntry.
11941 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011942 if (vmx->emulation_required) {
11943 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011944 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011945 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011946
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011947 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011948 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011949 entry_failure_code))
11950 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011951
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011952 if (!enable_ept)
11953 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11954
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011955 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11956 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011957 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011958}
11959
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011960static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11961{
11962 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11963 nested_cpu_has_virtual_nmis(vmcs12))
11964 return -EINVAL;
11965
11966 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11967 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11968 return -EINVAL;
11969
11970 return 0;
11971}
11972
Jim Mattsonca0bde22016-11-30 12:03:46 -080011973static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11974{
11975 struct vcpu_vmx *vmx = to_vmx(vcpu);
11976
11977 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11978 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11979 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11980
Jim Mattson56a20512017-07-06 16:33:06 -070011981 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11982 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11983
Jim Mattsonca0bde22016-11-30 12:03:46 -080011984 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11985 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11986
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011987 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11988 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11989
Jim Mattson712b12d2017-08-24 13:24:47 -070011990 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11991 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11992
Jim Mattsonca0bde22016-11-30 12:03:46 -080011993 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11994 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11995
11996 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11997 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11998
Bandan Dasc5f983f2017-05-05 15:25:14 -040011999 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12000 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12001
Liran Alona8a7c022018-06-23 02:35:06 +030012002 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12003 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12004
Jim Mattsonca0bde22016-11-30 12:03:46 -080012005 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012006 vmx->nested.msrs.procbased_ctls_low,
12007 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012008 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12009 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012010 vmx->nested.msrs.secondary_ctls_low,
12011 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012012 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012013 vmx->nested.msrs.pinbased_ctls_low,
12014 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012015 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012016 vmx->nested.msrs.exit_ctls_low,
12017 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012018 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012019 vmx->nested.msrs.entry_ctls_low,
12020 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012021 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12022
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012023 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012024 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12025
Bandan Das41ab9372017-08-03 15:54:43 -040012026 if (nested_cpu_has_vmfunc(vmcs12)) {
12027 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012028 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012029 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12030
12031 if (nested_cpu_has_eptp_switching(vmcs12)) {
12032 if (!nested_cpu_has_ept(vmcs12) ||
12033 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12034 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12035 }
12036 }
Bandan Das27c42a12017-08-03 15:54:42 -040012037
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012038 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12039 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12040
Jim Mattsonca0bde22016-11-30 12:03:46 -080012041 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12042 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12043 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12044 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12045
Marc Orr04473782018-06-20 17:21:29 -070012046 /*
12047 * From the Intel SDM, volume 3:
12048 * Fields relevant to VM-entry event injection must be set properly.
12049 * These fields are the VM-entry interruption-information field, the
12050 * VM-entry exception error code, and the VM-entry instruction length.
12051 */
12052 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12053 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12054 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12055 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12056 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12057 bool should_have_error_code;
12058 bool urg = nested_cpu_has2(vmcs12,
12059 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12060 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12061
12062 /* VM-entry interruption-info field: interruption type */
12063 if (intr_type == INTR_TYPE_RESERVED ||
12064 (intr_type == INTR_TYPE_OTHER_EVENT &&
12065 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12066 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12067
12068 /* VM-entry interruption-info field: vector */
12069 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12070 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12071 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12072 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12073
12074 /* VM-entry interruption-info field: deliver error code */
12075 should_have_error_code =
12076 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12077 x86_exception_has_error_code(vector);
12078 if (has_error_code != should_have_error_code)
12079 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12080
12081 /* VM-entry exception error code */
12082 if (has_error_code &&
12083 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12084 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12085
12086 /* VM-entry interruption-info field: reserved bits */
12087 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12088 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12089
12090 /* VM-entry instruction length */
12091 switch (intr_type) {
12092 case INTR_TYPE_SOFT_EXCEPTION:
12093 case INTR_TYPE_SOFT_INTR:
12094 case INTR_TYPE_PRIV_SW_EXCEPTION:
12095 if ((vmcs12->vm_entry_instruction_len > 15) ||
12096 (vmcs12->vm_entry_instruction_len == 0 &&
12097 !nested_cpu_has_zero_length_injection(vcpu)))
12098 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12099 }
12100 }
12101
Jim Mattsonca0bde22016-11-30 12:03:46 -080012102 return 0;
12103}
12104
Liran Alonf145d902018-06-23 02:35:07 +030012105static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12106 struct vmcs12 *vmcs12)
12107{
12108 int r;
12109 struct page *page;
12110 struct vmcs12 *shadow;
12111
12112 if (vmcs12->vmcs_link_pointer == -1ull)
12113 return 0;
12114
12115 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12116 return -EINVAL;
12117
12118 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12119 if (is_error_page(page))
12120 return -EINVAL;
12121
12122 r = 0;
12123 shadow = kmap(page);
12124 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12125 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12126 r = -EINVAL;
12127 kunmap(page);
12128 kvm_release_page_clean(page);
12129 return r;
12130}
12131
Jim Mattsonca0bde22016-11-30 12:03:46 -080012132static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12133 u32 *exit_qual)
12134{
12135 bool ia32e;
12136
12137 *exit_qual = ENTRY_FAIL_DEFAULT;
12138
12139 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12140 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12141 return 1;
12142
Liran Alonf145d902018-06-23 02:35:07 +030012143 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012144 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12145 return 1;
12146 }
12147
12148 /*
12149 * If the load IA32_EFER VM-entry control is 1, the following checks
12150 * are performed on the field for the IA32_EFER MSR:
12151 * - Bits reserved in the IA32_EFER MSR must be 0.
12152 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12153 * the IA-32e mode guest VM-exit control. It must also be identical
12154 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12155 * CR0.PG) is 1.
12156 */
12157 if (to_vmx(vcpu)->nested.nested_run_pending &&
12158 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12159 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12160 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12161 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12162 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12163 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12164 return 1;
12165 }
12166
12167 /*
12168 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12169 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12170 * the values of the LMA and LME bits in the field must each be that of
12171 * the host address-space size VM-exit control.
12172 */
12173 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12174 ia32e = (vmcs12->vm_exit_controls &
12175 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12176 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12177 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12178 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12179 return 1;
12180 }
12181
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012182 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12183 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12184 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12185 return 1;
12186
Jim Mattsonca0bde22016-11-30 12:03:46 -080012187 return 0;
12188}
12189
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012190/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012191 * If exit_qual is NULL, this is being called from state restore (either RSM
12192 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012193 */
12194static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012195{
12196 struct vcpu_vmx *vmx = to_vmx(vcpu);
12197 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012198 bool from_vmentry = !!exit_qual;
12199 u32 dummy_exit_qual;
12200 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012201
Jim Mattson858e25c2016-11-30 12:03:47 -080012202 enter_guest_mode(vcpu);
12203
12204 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12205 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12206
Jim Mattsonde3a0022017-11-27 17:22:25 -060012207 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012208 vmx_segment_cache_clear(vmx);
12209
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012210 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12211 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12212
12213 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012214 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012215 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012216
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012217 if (from_vmentry) {
12218 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012219
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012220 r = EXIT_REASON_MSR_LOAD_FAIL;
12221 *exit_qual = nested_vmx_load_msr(vcpu,
12222 vmcs12->vm_entry_msr_load_addr,
12223 vmcs12->vm_entry_msr_load_count);
12224 if (*exit_qual)
12225 goto fail;
12226 } else {
12227 /*
12228 * The MMU is not initialized to point at the right entities yet and
12229 * "get pages" would need to read data from the guest (i.e. we will
12230 * need to perform gpa to hpa translation). Request a call
12231 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12232 * have already been set at vmentry time and should not be reset.
12233 */
12234 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12235 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012236
Jim Mattson858e25c2016-11-30 12:03:47 -080012237 /*
12238 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12239 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12240 * returned as far as L1 is concerned. It will only return (and set
12241 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12242 */
12243 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012244
12245fail:
12246 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12247 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12248 leave_guest_mode(vcpu);
12249 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012250 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012251}
12252
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012253/*
12254 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12255 * for running an L2 nested guest.
12256 */
12257static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12258{
12259 struct vmcs12 *vmcs12;
12260 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012261 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012262 u32 exit_qual;
12263 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012264
Kyle Hueyeb277562016-11-29 12:40:39 -080012265 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012266 return 1;
12267
Kyle Hueyeb277562016-11-29 12:40:39 -080012268 if (!nested_vmx_check_vmcs12(vcpu))
12269 goto out;
12270
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012271 vmcs12 = get_vmcs12(vcpu);
12272
Liran Alona6192d42018-06-23 02:35:04 +030012273 /*
12274 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12275 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12276 * rather than RFLAGS.ZF, and no error number is stored to the
12277 * VM-instruction error field.
12278 */
12279 if (vmcs12->hdr.shadow_vmcs) {
12280 nested_vmx_failInvalid(vcpu);
12281 goto out;
12282 }
12283
Abel Gordon012f83c2013-04-18 14:39:25 +030012284 if (enable_shadow_vmcs)
12285 copy_shadow_to_vmcs12(vmx);
12286
Nadav Har'El7c177932011-05-25 23:12:04 +030012287 /*
12288 * The nested entry process starts with enforcing various prerequisites
12289 * on vmcs12 as required by the Intel SDM, and act appropriately when
12290 * they fail: As the SDM explains, some conditions should cause the
12291 * instruction to fail, while others will cause the instruction to seem
12292 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12293 * To speed up the normal (success) code path, we should avoid checking
12294 * for misconfigurations which will anyway be caught by the processor
12295 * when using the merged vmcs02.
12296 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012297 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12298 nested_vmx_failValid(vcpu,
12299 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12300 goto out;
12301 }
12302
Nadav Har'El7c177932011-05-25 23:12:04 +030012303 if (vmcs12->launch_state == launch) {
12304 nested_vmx_failValid(vcpu,
12305 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12306 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012307 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012308 }
12309
Jim Mattsonca0bde22016-11-30 12:03:46 -080012310 ret = check_vmentry_prereqs(vcpu, vmcs12);
12311 if (ret) {
12312 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012313 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012314 }
12315
Nadav Har'El7c177932011-05-25 23:12:04 +030012316 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012317 * After this point, the trap flag no longer triggers a singlestep trap
12318 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12319 * This is not 100% correct; for performance reasons, we delegate most
12320 * of the checks on host state to the processor. If those fail,
12321 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012322 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012323 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012324
Jim Mattsonca0bde22016-11-30 12:03:46 -080012325 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12326 if (ret) {
12327 nested_vmx_entry_failure(vcpu, vmcs12,
12328 EXIT_REASON_INVALID_STATE, exit_qual);
12329 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012330 }
12331
12332 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012333 * We're finally done with prerequisite checking, and can start with
12334 * the nested entry.
12335 */
12336
Jim Mattson6514dc32018-04-26 16:09:12 -070012337 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012338 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012339 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012340 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012341 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012342 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012343 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012344
Chao Gao135a06c2018-02-11 10:06:30 +080012345 /*
Liran Alon61ada742018-06-23 02:35:08 +030012346 * Must happen outside of enter_vmx_non_root_mode() as it will
12347 * also be used as part of restoring nVMX state for
12348 * snapshot restore (migration).
12349 *
12350 * In this flow, it is assumed that vmcs12 cache was
12351 * trasferred as part of captured nVMX state and should
12352 * therefore not be read from guest memory (which may not
12353 * exist on destination host yet).
12354 */
12355 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12356
12357 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012358 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12359 * by event injection, halt vcpu.
12360 */
12361 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012362 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12363 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012364 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012365 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012366 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012367
12368out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012369 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012370}
12371
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012372/*
12373 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12374 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12375 * This function returns the new value we should put in vmcs12.guest_cr0.
12376 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12377 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12378 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12379 * didn't trap the bit, because if L1 did, so would L0).
12380 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12381 * been modified by L2, and L1 knows it. So just leave the old value of
12382 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12383 * isn't relevant, because if L0 traps this bit it can set it to anything.
12384 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12385 * changed these bits, and therefore they need to be updated, but L0
12386 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12387 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12388 */
12389static inline unsigned long
12390vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12391{
12392 return
12393 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12394 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12395 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12396 vcpu->arch.cr0_guest_owned_bits));
12397}
12398
12399static inline unsigned long
12400vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12401{
12402 return
12403 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12404 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12405 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12406 vcpu->arch.cr4_guest_owned_bits));
12407}
12408
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012409static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12410 struct vmcs12 *vmcs12)
12411{
12412 u32 idt_vectoring;
12413 unsigned int nr;
12414
Wanpeng Li664f8e22017-08-24 03:35:09 -070012415 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012416 nr = vcpu->arch.exception.nr;
12417 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12418
12419 if (kvm_exception_is_soft(nr)) {
12420 vmcs12->vm_exit_instruction_len =
12421 vcpu->arch.event_exit_inst_len;
12422 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12423 } else
12424 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12425
12426 if (vcpu->arch.exception.has_error_code) {
12427 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12428 vmcs12->idt_vectoring_error_code =
12429 vcpu->arch.exception.error_code;
12430 }
12431
12432 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012433 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012434 vmcs12->idt_vectoring_info_field =
12435 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012436 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012437 nr = vcpu->arch.interrupt.nr;
12438 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12439
12440 if (vcpu->arch.interrupt.soft) {
12441 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12442 vmcs12->vm_entry_instruction_len =
12443 vcpu->arch.event_exit_inst_len;
12444 } else
12445 idt_vectoring |= INTR_TYPE_EXT_INTR;
12446
12447 vmcs12->idt_vectoring_info_field = idt_vectoring;
12448 }
12449}
12450
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012451static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12452{
12453 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012454 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012455 bool block_nested_events =
12456 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012457
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012458 if (vcpu->arch.exception.pending &&
12459 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012460 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012461 return -EBUSY;
12462 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012463 return 0;
12464 }
12465
Jan Kiszkaf4124502014-03-07 20:03:13 +010012466 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12467 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012468 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012469 return -EBUSY;
12470 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12471 return 0;
12472 }
12473
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012474 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012475 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012476 return -EBUSY;
12477 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12478 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12479 INTR_INFO_VALID_MASK, 0);
12480 /*
12481 * The NMI-triggered VM exit counts as injection:
12482 * clear this one and block further NMIs.
12483 */
12484 vcpu->arch.nmi_pending = 0;
12485 vmx_set_nmi_mask(vcpu, true);
12486 return 0;
12487 }
12488
12489 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12490 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012491 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012492 return -EBUSY;
12493 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012494 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012495 }
12496
David Hildenbrand6342c502017-01-25 11:58:58 +010012497 vmx_complete_nested_posted_interrupt(vcpu);
12498 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012499}
12500
Jan Kiszkaf4124502014-03-07 20:03:13 +010012501static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12502{
12503 ktime_t remaining =
12504 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12505 u64 value;
12506
12507 if (ktime_to_ns(remaining) <= 0)
12508 return 0;
12509
12510 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12511 do_div(value, 1000000);
12512 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12513}
12514
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012515/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012516 * Update the guest state fields of vmcs12 to reflect changes that
12517 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12518 * VM-entry controls is also updated, since this is really a guest
12519 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012520 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012521static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012522{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012523 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12524 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12525
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012526 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12527 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12528 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12529
12530 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12531 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12532 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12533 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12534 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12535 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12536 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12537 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12538 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12539 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12540 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12541 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12542 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12543 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12544 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12545 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12546 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12547 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12548 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12549 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12550 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12551 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12552 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12553 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12554 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12555 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12556 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12557 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12558 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12559 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12560 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12561 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12562 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12563 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12564 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12565 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12566
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012567 vmcs12->guest_interruptibility_info =
12568 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12569 vmcs12->guest_pending_dbg_exceptions =
12570 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012571 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12572 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12573 else
12574 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012575
Jan Kiszkaf4124502014-03-07 20:03:13 +010012576 if (nested_cpu_has_preemption_timer(vmcs12)) {
12577 if (vmcs12->vm_exit_controls &
12578 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12579 vmcs12->vmx_preemption_timer_value =
12580 vmx_get_preemption_timer_value(vcpu);
12581 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12582 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012583
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012584 /*
12585 * In some cases (usually, nested EPT), L2 is allowed to change its
12586 * own CR3 without exiting. If it has changed it, we must keep it.
12587 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12588 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12589 *
12590 * Additionally, restore L2's PDPTR to vmcs12.
12591 */
12592 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012593 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012594 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12595 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12596 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12597 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12598 }
12599
Jim Mattsond281e132017-06-01 12:44:46 -070012600 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012601
Wincy Van608406e2015-02-03 23:57:51 +080012602 if (nested_cpu_has_vid(vmcs12))
12603 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12604
Jan Kiszkac18911a2013-03-13 16:06:41 +010012605 vmcs12->vm_entry_controls =
12606 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012607 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012608
Jan Kiszka2996fca2014-06-16 13:59:43 +020012609 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12610 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12611 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12612 }
12613
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012614 /* TODO: These cannot have changed unless we have MSR bitmaps and
12615 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012616 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012617 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012618 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12619 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012620 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12621 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12622 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012623 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012624 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012625}
12626
12627/*
12628 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12629 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12630 * and this function updates it to reflect the changes to the guest state while
12631 * L2 was running (and perhaps made some exits which were handled directly by L0
12632 * without going back to L1), and to reflect the exit reason.
12633 * Note that we do not have to copy here all VMCS fields, just those that
12634 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12635 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12636 * which already writes to vmcs12 directly.
12637 */
12638static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12639 u32 exit_reason, u32 exit_intr_info,
12640 unsigned long exit_qualification)
12641{
12642 /* update guest state fields: */
12643 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012644
12645 /* update exit information fields: */
12646
Jan Kiszka533558b2014-01-04 18:47:20 +010012647 vmcs12->vm_exit_reason = exit_reason;
12648 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012649 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012650
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012651 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012652 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12653 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12654
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012655 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012656 vmcs12->launch_state = 1;
12657
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012658 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12659 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012660 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012661
12662 /*
12663 * Transfer the event that L0 or L1 may wanted to inject into
12664 * L2 to IDT_VECTORING_INFO_FIELD.
12665 */
12666 vmcs12_save_pending_event(vcpu, vmcs12);
12667 }
12668
12669 /*
12670 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12671 * preserved above and would only end up incorrectly in L1.
12672 */
12673 vcpu->arch.nmi_injected = false;
12674 kvm_clear_exception_queue(vcpu);
12675 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012676}
12677
Wanpeng Li5af41572017-11-05 16:54:49 -080012678static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12679 struct vmcs12 *vmcs12)
12680{
12681 u32 entry_failure_code;
12682
12683 nested_ept_uninit_mmu_context(vcpu);
12684
12685 /*
12686 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12687 * couldn't have changed.
12688 */
12689 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12690 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12691
12692 if (!enable_ept)
12693 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12694}
12695
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012696/*
12697 * A part of what we need to when the nested L2 guest exits and we want to
12698 * run its L1 parent, is to reset L1's guest state to the host state specified
12699 * in vmcs12.
12700 * This function is to be called not only on normal nested exit, but also on
12701 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12702 * Failures During or After Loading Guest State").
12703 * This function should be called when the active VMCS is L1's (vmcs01).
12704 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012705static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12706 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012707{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012708 struct kvm_segment seg;
12709
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012710 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12711 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012712 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012713 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12714 else
12715 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12716 vmx_set_efer(vcpu, vcpu->arch.efer);
12717
12718 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12719 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012720 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012721 /*
12722 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012723 * actually changed, because vmx_set_cr0 refers to efer set above.
12724 *
12725 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12726 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012727 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012728 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012729 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012730
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012731 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012732 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012733 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012734
Wanpeng Li5af41572017-11-05 16:54:49 -080012735 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012736
Liran Alon6f1e03b2018-05-22 17:16:14 +030012737 /*
12738 * If vmcs01 don't use VPID, CPU flushes TLB on every
12739 * VMEntry/VMExit. Thus, no need to flush TLB.
12740 *
12741 * If vmcs12 uses VPID, TLB entries populated by L2 are
12742 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12743 * with vmx->vpid. Thus, no need to flush TLB.
12744 *
12745 * Therefore, flush TLB only in case vmcs01 uses VPID and
12746 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12747 * are both tagged with vmx->vpid.
12748 */
12749 if (enable_vpid &&
12750 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012751 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012752 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012753
12754 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12755 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12756 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12757 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12758 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012759 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12760 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012761
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012762 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12763 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12764 vmcs_write64(GUEST_BNDCFGS, 0);
12765
Jan Kiszka44811c02013-08-04 17:17:27 +020012766 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012767 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012768 vcpu->arch.pat = vmcs12->host_ia32_pat;
12769 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012770 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12771 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12772 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012773
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012774 /* Set L1 segment info according to Intel SDM
12775 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12776 seg = (struct kvm_segment) {
12777 .base = 0,
12778 .limit = 0xFFFFFFFF,
12779 .selector = vmcs12->host_cs_selector,
12780 .type = 11,
12781 .present = 1,
12782 .s = 1,
12783 .g = 1
12784 };
12785 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12786 seg.l = 1;
12787 else
12788 seg.db = 1;
12789 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12790 seg = (struct kvm_segment) {
12791 .base = 0,
12792 .limit = 0xFFFFFFFF,
12793 .type = 3,
12794 .present = 1,
12795 .s = 1,
12796 .db = 1,
12797 .g = 1
12798 };
12799 seg.selector = vmcs12->host_ds_selector;
12800 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12801 seg.selector = vmcs12->host_es_selector;
12802 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12803 seg.selector = vmcs12->host_ss_selector;
12804 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12805 seg.selector = vmcs12->host_fs_selector;
12806 seg.base = vmcs12->host_fs_base;
12807 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12808 seg.selector = vmcs12->host_gs_selector;
12809 seg.base = vmcs12->host_gs_base;
12810 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12811 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012812 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012813 .limit = 0x67,
12814 .selector = vmcs12->host_tr_selector,
12815 .type = 11,
12816 .present = 1
12817 };
12818 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12819
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012820 kvm_set_dr(vcpu, 7, 0x400);
12821 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012822
Wincy Van3af18d92015-02-03 23:49:31 +080012823 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012824 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012825
Wincy Vanff651cb2014-12-11 08:52:58 +030012826 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12827 vmcs12->vm_exit_msr_load_count))
12828 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012829}
12830
12831/*
12832 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12833 * and modify vmcs12 to make it see what it would expect to see there if
12834 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12835 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012836static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12837 u32 exit_intr_info,
12838 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012839{
12840 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012841 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12842
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012843 /* trying to cancel vmlaunch/vmresume is a bug */
12844 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12845
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012846 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012847 * The only expected VM-instruction error is "VM entry with
12848 * invalid control field(s)." Anything else indicates a
12849 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012850 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012851 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12852 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12853
12854 leave_guest_mode(vcpu);
12855
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012856 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12857 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12858
Jim Mattson4f350c62017-09-14 16:31:44 -070012859 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012860 if (exit_reason == -1)
12861 sync_vmcs12(vcpu, vmcs12);
12862 else
12863 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12864 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012865
Liran Alon61ada742018-06-23 02:35:08 +030012866 /*
12867 * Must happen outside of sync_vmcs12() as it will
12868 * also be used to capture vmcs12 cache as part of
12869 * capturing nVMX state for snapshot (migration).
12870 *
12871 * Otherwise, this flush will dirty guest memory at a
12872 * point it is already assumed by user-space to be
12873 * immutable.
12874 */
12875 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12876
Jim Mattson4f350c62017-09-14 16:31:44 -070012877 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12878 vmcs12->vm_exit_msr_store_count))
12879 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012880 }
12881
Jim Mattson4f350c62017-09-14 16:31:44 -070012882 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012883 vm_entry_controls_reset_shadow(vmx);
12884 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012885 vmx_segment_cache_clear(vmx);
12886
Paolo Bonzini93140062016-07-06 13:23:51 +020012887 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012888 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12889 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012890 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012891 if (vmx->hv_deadline_tsc == -1)
12892 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12893 PIN_BASED_VMX_PREEMPTION_TIMER);
12894 else
12895 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12896 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012897 if (kvm_has_tsc_control)
12898 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012899
Jim Mattson8d860bb2018-05-09 16:56:05 -040012900 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12901 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12902 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012903 } else if (!nested_cpu_has_ept(vmcs12) &&
12904 nested_cpu_has2(vmcs12,
12905 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012906 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012907 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012908
12909 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12910 vmx->host_rsp = 0;
12911
12912 /* Unpin physical memory we referred to in vmcs02 */
12913 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012914 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012915 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012916 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012917 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012918 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012919 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012920 }
Wincy Van705699a2015-02-03 23:58:17 +080012921 if (vmx->nested.pi_desc_page) {
12922 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012923 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012924 vmx->nested.pi_desc_page = NULL;
12925 vmx->nested.pi_desc = NULL;
12926 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012927
12928 /*
Tang Chen38b99172014-09-24 15:57:54 +080012929 * We are now running in L2, mmu_notifier will force to reload the
12930 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12931 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012932 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012933
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012934 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012935 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012936
12937 /* in case we halted in L2 */
12938 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012939
12940 if (likely(!vmx->fail)) {
12941 /*
12942 * TODO: SDM says that with acknowledge interrupt on
12943 * exit, bit 31 of the VM-exit interrupt information
12944 * (valid interrupt) is always set to 1 on
12945 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12946 * need kvm_cpu_has_interrupt(). See the commit
12947 * message for details.
12948 */
12949 if (nested_exit_intr_ack_set(vcpu) &&
12950 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12951 kvm_cpu_has_interrupt(vcpu)) {
12952 int irq = kvm_cpu_get_interrupt(vcpu);
12953 WARN_ON(irq < 0);
12954 vmcs12->vm_exit_intr_info = irq |
12955 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12956 }
12957
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012958 if (exit_reason != -1)
12959 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12960 vmcs12->exit_qualification,
12961 vmcs12->idt_vectoring_info_field,
12962 vmcs12->vm_exit_intr_info,
12963 vmcs12->vm_exit_intr_error_code,
12964 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012965
12966 load_vmcs12_host_state(vcpu, vmcs12);
12967
12968 return;
12969 }
12970
12971 /*
12972 * After an early L2 VM-entry failure, we're now back
12973 * in L1 which thinks it just finished a VMLAUNCH or
12974 * VMRESUME instruction, so we need to set the failure
12975 * flag and the VM-instruction error field of the VMCS
12976 * accordingly.
12977 */
12978 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012979
12980 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12981
Jim Mattson4f350c62017-09-14 16:31:44 -070012982 /*
12983 * The emulated instruction was already skipped in
12984 * nested_vmx_run, but the updated RIP was never
12985 * written back to the vmcs01.
12986 */
12987 skip_emulated_instruction(vcpu);
12988 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012989}
12990
Nadav Har'El7c177932011-05-25 23:12:04 +030012991/*
Jan Kiszka42124922014-01-04 18:47:19 +010012992 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12993 */
12994static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12995{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012996 if (is_guest_mode(vcpu)) {
12997 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012998 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012999 }
Jan Kiszka42124922014-01-04 18:47:19 +010013000 free_nested(to_vmx(vcpu));
13001}
13002
13003/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013004 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13005 * 23.7 "VM-entry failures during or after loading guest state" (this also
13006 * lists the acceptable exit-reason and exit-qualification parameters).
13007 * It should only be called before L2 actually succeeded to run, and when
13008 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13009 */
13010static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13011 struct vmcs12 *vmcs12,
13012 u32 reason, unsigned long qualification)
13013{
13014 load_vmcs12_host_state(vcpu, vmcs12);
13015 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13016 vmcs12->exit_qualification = qualification;
13017 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013018 if (enable_shadow_vmcs)
13019 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013020}
13021
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013022static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13023 struct x86_instruction_info *info,
13024 enum x86_intercept_stage stage)
13025{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013026 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13027 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13028
13029 /*
13030 * RDPID causes #UD if disabled through secondary execution controls.
13031 * Because it is marked as EmulateOnUD, we need to intercept it here.
13032 */
13033 if (info->intercept == x86_intercept_rdtscp &&
13034 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13035 ctxt->exception.vector = UD_VECTOR;
13036 ctxt->exception.error_code_valid = false;
13037 return X86EMUL_PROPAGATE_FAULT;
13038 }
13039
13040 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013041 return X86EMUL_CONTINUE;
13042}
13043
Yunhong Jiang64672c92016-06-13 14:19:59 -070013044#ifdef CONFIG_X86_64
13045/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13046static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13047 u64 divisor, u64 *result)
13048{
13049 u64 low = a << shift, high = a >> (64 - shift);
13050
13051 /* To avoid the overflow on divq */
13052 if (high >= divisor)
13053 return 1;
13054
13055 /* Low hold the result, high hold rem which is discarded */
13056 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13057 "rm" (divisor), "0" (low), "1" (high));
13058 *result = low;
13059
13060 return 0;
13061}
13062
13063static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13064{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013065 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013066 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013067
13068 if (kvm_mwait_in_guest(vcpu->kvm))
13069 return -EOPNOTSUPP;
13070
13071 vmx = to_vmx(vcpu);
13072 tscl = rdtsc();
13073 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13074 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013075 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13076
13077 if (delta_tsc > lapic_timer_advance_cycles)
13078 delta_tsc -= lapic_timer_advance_cycles;
13079 else
13080 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013081
13082 /* Convert to host delta tsc if tsc scaling is enabled */
13083 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13084 u64_shl_div_u64(delta_tsc,
13085 kvm_tsc_scaling_ratio_frac_bits,
13086 vcpu->arch.tsc_scaling_ratio,
13087 &delta_tsc))
13088 return -ERANGE;
13089
13090 /*
13091 * If the delta tsc can't fit in the 32 bit after the multi shift,
13092 * we can't use the preemption timer.
13093 * It's possible that it fits on later vmentries, but checking
13094 * on every vmentry is costly so we just use an hrtimer.
13095 */
13096 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13097 return -ERANGE;
13098
13099 vmx->hv_deadline_tsc = tscl + delta_tsc;
13100 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13101 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070013102
13103 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013104}
13105
13106static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13107{
13108 struct vcpu_vmx *vmx = to_vmx(vcpu);
13109 vmx->hv_deadline_tsc = -1;
13110 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13111 PIN_BASED_VMX_PREEMPTION_TIMER);
13112}
13113#endif
13114
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013115static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013116{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013117 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013118 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013119}
13120
Kai Huang843e4332015-01-28 10:54:28 +080013121static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13122 struct kvm_memory_slot *slot)
13123{
13124 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13125 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13126}
13127
13128static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13129 struct kvm_memory_slot *slot)
13130{
13131 kvm_mmu_slot_set_dirty(kvm, slot);
13132}
13133
13134static void vmx_flush_log_dirty(struct kvm *kvm)
13135{
13136 kvm_flush_pml_buffers(kvm);
13137}
13138
Bandan Dasc5f983f2017-05-05 15:25:14 -040013139static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13140{
13141 struct vmcs12 *vmcs12;
13142 struct vcpu_vmx *vmx = to_vmx(vcpu);
13143 gpa_t gpa;
13144 struct page *page = NULL;
13145 u64 *pml_address;
13146
13147 if (is_guest_mode(vcpu)) {
13148 WARN_ON_ONCE(vmx->nested.pml_full);
13149
13150 /*
13151 * Check if PML is enabled for the nested guest.
13152 * Whether eptp bit 6 is set is already checked
13153 * as part of A/D emulation.
13154 */
13155 vmcs12 = get_vmcs12(vcpu);
13156 if (!nested_cpu_has_pml(vmcs12))
13157 return 0;
13158
Dan Carpenter47698862017-05-10 22:43:17 +030013159 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013160 vmx->nested.pml_full = true;
13161 return 1;
13162 }
13163
13164 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13165
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013166 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13167 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013168 return 0;
13169
13170 pml_address = kmap(page);
13171 pml_address[vmcs12->guest_pml_index--] = gpa;
13172 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013173 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013174 }
13175
13176 return 0;
13177}
13178
Kai Huang843e4332015-01-28 10:54:28 +080013179static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13180 struct kvm_memory_slot *memslot,
13181 gfn_t offset, unsigned long mask)
13182{
13183 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13184}
13185
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013186static void __pi_post_block(struct kvm_vcpu *vcpu)
13187{
13188 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13189 struct pi_desc old, new;
13190 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013191
13192 do {
13193 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013194 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13195 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013196
13197 dest = cpu_physical_id(vcpu->cpu);
13198
13199 if (x2apic_enabled())
13200 new.ndst = dest;
13201 else
13202 new.ndst = (dest << 8) & 0xFF00;
13203
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013204 /* set 'NV' to 'notification vector' */
13205 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013206 } while (cmpxchg64(&pi_desc->control, old.control,
13207 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013208
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013209 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13210 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013211 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013212 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013213 vcpu->pre_pcpu = -1;
13214 }
13215}
13216
Feng Wuefc64402015-09-18 22:29:51 +080013217/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013218 * This routine does the following things for vCPU which is going
13219 * to be blocked if VT-d PI is enabled.
13220 * - Store the vCPU to the wakeup list, so when interrupts happen
13221 * we can find the right vCPU to wake up.
13222 * - Change the Posted-interrupt descriptor as below:
13223 * 'NDST' <-- vcpu->pre_pcpu
13224 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13225 * - If 'ON' is set during this process, which means at least one
13226 * interrupt is posted for this vCPU, we cannot block it, in
13227 * this case, return 1, otherwise, return 0.
13228 *
13229 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013230static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013231{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013232 unsigned int dest;
13233 struct pi_desc old, new;
13234 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13235
13236 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013237 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13238 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013239 return 0;
13240
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013241 WARN_ON(irqs_disabled());
13242 local_irq_disable();
13243 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13244 vcpu->pre_pcpu = vcpu->cpu;
13245 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13246 list_add_tail(&vcpu->blocked_vcpu_list,
13247 &per_cpu(blocked_vcpu_on_cpu,
13248 vcpu->pre_pcpu));
13249 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13250 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013251
13252 do {
13253 old.control = new.control = pi_desc->control;
13254
Feng Wubf9f6ac2015-09-18 22:29:55 +080013255 WARN((pi_desc->sn == 1),
13256 "Warning: SN field of posted-interrupts "
13257 "is set before blocking\n");
13258
13259 /*
13260 * Since vCPU can be preempted during this process,
13261 * vcpu->cpu could be different with pre_pcpu, we
13262 * need to set pre_pcpu as the destination of wakeup
13263 * notification event, then we can find the right vCPU
13264 * to wakeup in wakeup handler if interrupts happen
13265 * when the vCPU is in blocked state.
13266 */
13267 dest = cpu_physical_id(vcpu->pre_pcpu);
13268
13269 if (x2apic_enabled())
13270 new.ndst = dest;
13271 else
13272 new.ndst = (dest << 8) & 0xFF00;
13273
13274 /* set 'NV' to 'wakeup vector' */
13275 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013276 } while (cmpxchg64(&pi_desc->control, old.control,
13277 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013278
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013279 /* We should not block the vCPU if an interrupt is posted for it. */
13280 if (pi_test_on(pi_desc) == 1)
13281 __pi_post_block(vcpu);
13282
13283 local_irq_enable();
13284 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013285}
13286
Yunhong Jiangbc225122016-06-13 14:19:58 -070013287static int vmx_pre_block(struct kvm_vcpu *vcpu)
13288{
13289 if (pi_pre_block(vcpu))
13290 return 1;
13291
Yunhong Jiang64672c92016-06-13 14:19:59 -070013292 if (kvm_lapic_hv_timer_in_use(vcpu))
13293 kvm_lapic_switch_to_sw_timer(vcpu);
13294
Yunhong Jiangbc225122016-06-13 14:19:58 -070013295 return 0;
13296}
13297
13298static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013299{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013300 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013301 return;
13302
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013303 WARN_ON(irqs_disabled());
13304 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013305 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013306 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013307}
13308
Yunhong Jiangbc225122016-06-13 14:19:58 -070013309static void vmx_post_block(struct kvm_vcpu *vcpu)
13310{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013311 if (kvm_x86_ops->set_hv_timer)
13312 kvm_lapic_switch_to_hv_timer(vcpu);
13313
Yunhong Jiangbc225122016-06-13 14:19:58 -070013314 pi_post_block(vcpu);
13315}
13316
Feng Wubf9f6ac2015-09-18 22:29:55 +080013317/*
Feng Wuefc64402015-09-18 22:29:51 +080013318 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13319 *
13320 * @kvm: kvm
13321 * @host_irq: host irq of the interrupt
13322 * @guest_irq: gsi of the interrupt
13323 * @set: set or unset PI
13324 * returns 0 on success, < 0 on failure
13325 */
13326static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13327 uint32_t guest_irq, bool set)
13328{
13329 struct kvm_kernel_irq_routing_entry *e;
13330 struct kvm_irq_routing_table *irq_rt;
13331 struct kvm_lapic_irq irq;
13332 struct kvm_vcpu *vcpu;
13333 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013334 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013335
13336 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013337 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13338 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013339 return 0;
13340
13341 idx = srcu_read_lock(&kvm->irq_srcu);
13342 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013343 if (guest_irq >= irq_rt->nr_rt_entries ||
13344 hlist_empty(&irq_rt->map[guest_irq])) {
13345 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13346 guest_irq, irq_rt->nr_rt_entries);
13347 goto out;
13348 }
Feng Wuefc64402015-09-18 22:29:51 +080013349
13350 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13351 if (e->type != KVM_IRQ_ROUTING_MSI)
13352 continue;
13353 /*
13354 * VT-d PI cannot support posting multicast/broadcast
13355 * interrupts to a vCPU, we still use interrupt remapping
13356 * for these kind of interrupts.
13357 *
13358 * For lowest-priority interrupts, we only support
13359 * those with single CPU as the destination, e.g. user
13360 * configures the interrupts via /proc/irq or uses
13361 * irqbalance to make the interrupts single-CPU.
13362 *
13363 * We will support full lowest-priority interrupt later.
13364 */
13365
Radim Krčmář371313132016-07-12 22:09:27 +020013366 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013367 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13368 /*
13369 * Make sure the IRTE is in remapped mode if
13370 * we don't handle it in posted mode.
13371 */
13372 ret = irq_set_vcpu_affinity(host_irq, NULL);
13373 if (ret < 0) {
13374 printk(KERN_INFO
13375 "failed to back to remapped mode, irq: %u\n",
13376 host_irq);
13377 goto out;
13378 }
13379
Feng Wuefc64402015-09-18 22:29:51 +080013380 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013381 }
Feng Wuefc64402015-09-18 22:29:51 +080013382
13383 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13384 vcpu_info.vector = irq.vector;
13385
hu huajun2698d822018-04-11 15:16:40 +080013386 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013387 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13388
13389 if (set)
13390 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013391 else
Feng Wuefc64402015-09-18 22:29:51 +080013392 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013393
13394 if (ret < 0) {
13395 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13396 __func__);
13397 goto out;
13398 }
13399 }
13400
13401 ret = 0;
13402out:
13403 srcu_read_unlock(&kvm->irq_srcu, idx);
13404 return ret;
13405}
13406
Ashok Rajc45dcc72016-06-22 14:59:56 +080013407static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13408{
13409 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13410 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13411 FEATURE_CONTROL_LMCE;
13412 else
13413 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13414 ~FEATURE_CONTROL_LMCE;
13415}
13416
Ladi Prosek72d7b372017-10-11 16:54:41 +020013417static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13418{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013419 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13420 if (to_vmx(vcpu)->nested.nested_run_pending)
13421 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013422 return 1;
13423}
13424
Ladi Prosek0234bf82017-10-11 16:54:40 +020013425static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13426{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013427 struct vcpu_vmx *vmx = to_vmx(vcpu);
13428
13429 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13430 if (vmx->nested.smm.guest_mode)
13431 nested_vmx_vmexit(vcpu, -1, 0, 0);
13432
13433 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13434 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013435 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013436 return 0;
13437}
13438
13439static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13440{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013441 struct vcpu_vmx *vmx = to_vmx(vcpu);
13442 int ret;
13443
13444 if (vmx->nested.smm.vmxon) {
13445 vmx->nested.vmxon = true;
13446 vmx->nested.smm.vmxon = false;
13447 }
13448
13449 if (vmx->nested.smm.guest_mode) {
13450 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013451 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013452 vcpu->arch.hflags |= HF_SMM_MASK;
13453 if (ret)
13454 return ret;
13455
13456 vmx->nested.smm.guest_mode = false;
13457 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013458 return 0;
13459}
13460
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013461static int enable_smi_window(struct kvm_vcpu *vcpu)
13462{
13463 return 0;
13464}
13465
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013466static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13467 struct kvm_nested_state __user *user_kvm_nested_state,
13468 u32 user_data_size)
13469{
13470 struct vcpu_vmx *vmx;
13471 struct vmcs12 *vmcs12;
13472 struct kvm_nested_state kvm_state = {
13473 .flags = 0,
13474 .format = 0,
13475 .size = sizeof(kvm_state),
13476 .vmx.vmxon_pa = -1ull,
13477 .vmx.vmcs_pa = -1ull,
13478 };
13479
13480 if (!vcpu)
13481 return kvm_state.size + 2 * VMCS12_SIZE;
13482
13483 vmx = to_vmx(vcpu);
13484 vmcs12 = get_vmcs12(vcpu);
13485 if (nested_vmx_allowed(vcpu) &&
13486 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13487 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13488 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13489
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013490 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013491 kvm_state.size += VMCS12_SIZE;
13492
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013493 if (is_guest_mode(vcpu) &&
13494 nested_cpu_has_shadow_vmcs(vmcs12) &&
13495 vmcs12->vmcs_link_pointer != -1ull)
13496 kvm_state.size += VMCS12_SIZE;
13497 }
13498
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013499 if (vmx->nested.smm.vmxon)
13500 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13501
13502 if (vmx->nested.smm.guest_mode)
13503 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13504
13505 if (is_guest_mode(vcpu)) {
13506 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13507
13508 if (vmx->nested.nested_run_pending)
13509 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13510 }
13511 }
13512
13513 if (user_data_size < kvm_state.size)
13514 goto out;
13515
13516 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13517 return -EFAULT;
13518
13519 if (vmx->nested.current_vmptr == -1ull)
13520 goto out;
13521
13522 /*
13523 * When running L2, the authoritative vmcs12 state is in the
13524 * vmcs02. When running L1, the authoritative vmcs12 state is
13525 * in the shadow vmcs linked to vmcs01, unless
13526 * sync_shadow_vmcs is set, in which case, the authoritative
13527 * vmcs12 state is in the vmcs12 already.
13528 */
13529 if (is_guest_mode(vcpu))
13530 sync_vmcs12(vcpu, vmcs12);
13531 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13532 copy_shadow_to_vmcs12(vmx);
13533
13534 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13535 return -EFAULT;
13536
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013537 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13538 vmcs12->vmcs_link_pointer != -1ull) {
13539 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13540 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13541 return -EFAULT;
13542 }
13543
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013544out:
13545 return kvm_state.size;
13546}
13547
13548static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13549 struct kvm_nested_state __user *user_kvm_nested_state,
13550 struct kvm_nested_state *kvm_state)
13551{
13552 struct vcpu_vmx *vmx = to_vmx(vcpu);
13553 struct vmcs12 *vmcs12;
13554 u32 exit_qual;
13555 int ret;
13556
13557 if (kvm_state->format != 0)
13558 return -EINVAL;
13559
13560 if (!nested_vmx_allowed(vcpu))
13561 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13562
13563 if (kvm_state->vmx.vmxon_pa == -1ull) {
13564 if (kvm_state->vmx.smm.flags)
13565 return -EINVAL;
13566
13567 if (kvm_state->vmx.vmcs_pa != -1ull)
13568 return -EINVAL;
13569
13570 vmx_leave_nested(vcpu);
13571 return 0;
13572 }
13573
13574 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13575 return -EINVAL;
13576
13577 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13578 return -EINVAL;
13579
13580 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13581 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13582 return -EINVAL;
13583
13584 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13585 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13586 return -EINVAL;
13587
13588 if (kvm_state->vmx.smm.flags &
13589 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13590 return -EINVAL;
13591
13592 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13593 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13594 return -EINVAL;
13595
13596 vmx_leave_nested(vcpu);
13597 if (kvm_state->vmx.vmxon_pa == -1ull)
13598 return 0;
13599
13600 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13601 ret = enter_vmx_operation(vcpu);
13602 if (ret)
13603 return ret;
13604
13605 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13606
13607 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13608 vmx->nested.smm.vmxon = true;
13609 vmx->nested.vmxon = false;
13610
13611 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13612 vmx->nested.smm.guest_mode = true;
13613 }
13614
13615 vmcs12 = get_vmcs12(vcpu);
13616 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13617 return -EFAULT;
13618
Liran Alon392b2f22018-06-23 02:35:01 +030013619 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013620 return -EINVAL;
13621
13622 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13623 return 0;
13624
13625 vmx->nested.nested_run_pending =
13626 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13627
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013628 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13629 vmcs12->vmcs_link_pointer != -1ull) {
13630 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13631 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13632 return -EINVAL;
13633
13634 if (copy_from_user(shadow_vmcs12,
13635 user_kvm_nested_state->data + VMCS12_SIZE,
13636 sizeof(*vmcs12)))
13637 return -EFAULT;
13638
13639 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13640 !shadow_vmcs12->hdr.shadow_vmcs)
13641 return -EINVAL;
13642 }
13643
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013644 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13645 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13646 return -EINVAL;
13647
13648 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13649 vmx->nested.nested_run_pending = 1;
13650
13651 vmx->nested.dirty_vmcs12 = true;
13652 ret = enter_vmx_non_root_mode(vcpu, NULL);
13653 if (ret)
13654 return -EINVAL;
13655
13656 return 0;
13657}
13658
Kees Cook404f6aa2016-08-08 16:29:06 -070013659static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013660 .cpu_has_kvm_support = cpu_has_kvm_support,
13661 .disabled_by_bios = vmx_disabled_by_bios,
13662 .hardware_setup = hardware_setup,
13663 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013664 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013665 .hardware_enable = hardware_enable,
13666 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013667 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013668 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013669
Wanpeng Lib31c1142018-03-12 04:53:04 -070013670 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013671 .vm_alloc = vmx_vm_alloc,
13672 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013673
Avi Kivity6aa8b732006-12-10 02:21:36 -080013674 .vcpu_create = vmx_create_vcpu,
13675 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013676 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013677
Avi Kivity04d2cc72007-09-10 18:10:54 +030013678 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013679 .vcpu_load = vmx_vcpu_load,
13680 .vcpu_put = vmx_vcpu_put,
13681
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013682 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013683 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013684 .get_msr = vmx_get_msr,
13685 .set_msr = vmx_set_msr,
13686 .get_segment_base = vmx_get_segment_base,
13687 .get_segment = vmx_get_segment,
13688 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013689 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013690 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013691 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013692 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013693 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013694 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013695 .set_cr3 = vmx_set_cr3,
13696 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013697 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013698 .get_idt = vmx_get_idt,
13699 .set_idt = vmx_set_idt,
13700 .get_gdt = vmx_get_gdt,
13701 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013702 .get_dr6 = vmx_get_dr6,
13703 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013704 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013705 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013706 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013707 .get_rflags = vmx_get_rflags,
13708 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013709
Avi Kivity6aa8b732006-12-10 02:21:36 -080013710 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070013711 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013712
Avi Kivity6aa8b732006-12-10 02:21:36 -080013713 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013714 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013715 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013716 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13717 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013718 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013719 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013720 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013721 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013722 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013723 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013724 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013725 .get_nmi_mask = vmx_get_nmi_mask,
13726 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013727 .enable_nmi_window = enable_nmi_window,
13728 .enable_irq_window = enable_irq_window,
13729 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013730 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013731 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013732 .get_enable_apicv = vmx_get_enable_apicv,
13733 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013734 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013735 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013736 .hwapic_irr_update = vmx_hwapic_irr_update,
13737 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013738 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13739 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013740
Izik Eiduscbc94022007-10-25 00:29:55 +020013741 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013742 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013743 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013744 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013745
Avi Kivity586f9602010-11-18 13:09:54 +020013746 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013747
Sheng Yang17cc3932010-01-05 19:02:27 +080013748 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013749
13750 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013751
13752 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013753 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013754
13755 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013756
13757 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013758
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013759 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013760 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013761
13762 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013763
13764 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013765 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013766 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013767 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013768 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013769
13770 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013771
13772 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013773
13774 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13775 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13776 .flush_log_dirty = vmx_flush_log_dirty,
13777 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013778 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013779
Feng Wubf9f6ac2015-09-18 22:29:55 +080013780 .pre_block = vmx_pre_block,
13781 .post_block = vmx_post_block,
13782
Wei Huang25462f72015-06-19 15:45:05 +020013783 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013784
13785 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013786
13787#ifdef CONFIG_X86_64
13788 .set_hv_timer = vmx_set_hv_timer,
13789 .cancel_hv_timer = vmx_cancel_hv_timer,
13790#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013791
13792 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013793
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013794 .get_nested_state = vmx_get_nested_state,
13795 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013796 .get_vmcs12_pages = nested_get_vmcs12_pages,
13797
Ladi Prosek72d7b372017-10-11 16:54:41 +020013798 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013799 .pre_enter_smm = vmx_pre_enter_smm,
13800 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013801 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013802};
13803
13804static int __init vmx_init(void)
13805{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013806 int r;
13807
13808#if IS_ENABLED(CONFIG_HYPERV)
13809 /*
13810 * Enlightened VMCS usage should be recommended and the host needs
13811 * to support eVMCS v1 or above. We can also disable eVMCS support
13812 * with module parameter.
13813 */
13814 if (enlightened_vmcs &&
13815 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13816 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13817 KVM_EVMCS_VERSION) {
13818 int cpu;
13819
13820 /* Check that we have assist pages on all online CPUs */
13821 for_each_online_cpu(cpu) {
13822 if (!hv_get_vp_assist_page(cpu)) {
13823 enlightened_vmcs = false;
13824 break;
13825 }
13826 }
13827
13828 if (enlightened_vmcs) {
13829 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13830 static_branch_enable(&enable_evmcs);
13831 }
13832 } else {
13833 enlightened_vmcs = false;
13834 }
13835#endif
13836
13837 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013838 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013839 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013840 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013841
Dave Young2965faa2015-09-09 15:38:55 -070013842#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013843 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13844 crash_vmclear_local_loaded_vmcss);
13845#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013846 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013847
He, Qingfdef3ad2007-04-30 09:45:24 +030013848 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013849}
13850
13851static void __exit vmx_exit(void)
13852{
Dave Young2965faa2015-09-09 15:38:55 -070013853#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013854 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013855 synchronize_rcu();
13856#endif
13857
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013858 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013859
13860#if IS_ENABLED(CONFIG_HYPERV)
13861 if (static_branch_unlikely(&enable_evmcs)) {
13862 int cpu;
13863 struct hv_vp_assist_page *vp_ap;
13864 /*
13865 * Reset everything to support using non-enlightened VMCS
13866 * access later (e.g. when we reload the module with
13867 * enlightened_vmcs=0)
13868 */
13869 for_each_online_cpu(cpu) {
13870 vp_ap = hv_get_vp_assist_page(cpu);
13871
13872 if (!vp_ap)
13873 continue;
13874
13875 vp_ap->current_nested_vmcs = 0;
13876 vp_ap->enlighten_vmentry = 0;
13877 }
13878
13879 static_branch_disable(&enable_evmcs);
13880 }
13881#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013882}
13883
13884module_init(vmx_init)
13885module_exit(vmx_exit)